// 语句块

function addArduinoCod (Blockly) {
    Blockly.Arduino.test = function (block) {
        // If/elseif/else condition.
        return '111 \n';
    };
    Blockly.Arduino.serial_band_rate = function (block) {
        return 'serial_band_rate() \n';
    }
	//motordriverboard初始化
	 Blockly.Arduino['md_mdinit'] = function(block) {
		  var dropdown_version = this.getFieldValue('version');
		  Blockly.Arduino.definitions_['include_md_mdinit'] = '#include <Emakefun_MotorDriver.h>';
		  Blockly.Arduino.definitions_['md_mdinit'] = 'Emakefun_MotorDriver mMotorDriver = Emakefun_MotorDriver(0x60,'+dropdown_version+');';
		return '';
   }
   //串口波特率
	Blockly.Arduino['serialbeginsss'] = function(block) {
		var dropdown_serialbeginsssss = this.getFieldValue('serialbeginsssss');
		//Blockly.Arduino.definitions_['setup_serialbegin'] = '';
		return  'Serial.begin(' + dropdown_serialbeginsssss + '); \n';
}
	//串口打印字符
	Blockly.Arduino['serialprintsss'] = function(block) {
		var value_serialprintssss = Blockly.Arduino.valueToCode(this, 'serialprintssss', Blockly.Arduino.ORDER_ATOMIC);
		return  'Serial.println(' + value_serialprintssss.replace(/'/g, '"') + '); \n';
}
	//串口打印数字
		Blockly.Arduino['serialprintnum1sss'] = function(block) {
			var value_serialprintsssss = Blockly.Arduino.valueToCode(this, 'serialprintsssss', Blockly.Arduino.ORDER_ATOMIC);
			return  'Serial.println(' + value_serialprintsssss + '); \n';
}
	//舵机初始化
       Blockly.Arduino['md_servoinit'] = function(block) {
        var dropdown_servoports = this.getFieldValue('servoports');
		Blockly.Arduino.definitions_['md_servoinit_'+dropdown_servoports+''] = 'Emakefun_Servo *servo'+dropdown_servoports+' = mMotorDriver.getServo('+dropdown_servoports+');';
		return 'mMotorDriver.begin(50);\n';
   }
	//舵机角度
       Blockly.Arduino['md_servodegree'] = function(block) {
		   var dropdown_servoportss = this.getFieldValue('servoportss');
		var value_servodegrees = Blockly.Arduino.valueToCode(this, 'servodegrees', Blockly.Arduino.ORDER_ATOMIC);
		return 'servo'+dropdown_servoportss+'->writeServo('+value_servodegrees+');\n';
   }
	//直流电机初始化
       Blockly.Arduino['md_dcmotorinit'] = function(block) {
		   var dropdown_dcmotorports = this.getFieldValue('dcmotorports');
		   Blockly.Arduino.definitions_['md_dcmotorinit_'+dropdown_dcmotorports+''] = 'Emakefun_DCMotor *DCmotor_'+dropdown_dcmotorports+' = mMotorDriver.getMotor('+dropdown_dcmotorports+');';
		return 'mMotorDriver.begin(50);\n';
   }
   	//方向速度
       Blockly.Arduino['md_dcmotorspeed'] = function(block) {
		   var dropdown_dcmotorportss = this.getFieldValue('dcmotorportss');
		   var dropdown_dcmotordirections = this.getFieldValue('dcmotordirections');
		   var value_dcmotorspeeds = Blockly.Arduino.valueToCode(this, 'dcmotorspeeds', Blockly.Arduino.ORDER_ATOMIC);
		   //Blockly.Arduino.definitions_['md_dcmotorinit'] = 'Emakefun_Servo *servo'+dropdown_dcmotorports+' = mMotorDriver.getMotor('+dropdown_dcmotorports+');';
		return 'DCmotor_'+dropdown_dcmotorportss+'->setSpeed('+value_dcmotorspeeds+');\nDCmotor_'+dropdown_dcmotorportss+'->run('+dropdown_dcmotordirections+');\n';
   }
   //停止直流电机
     Blockly.Arduino['md_stodcpmotor'] = function(block) {
		   var dropdown_dcmotorportsss = this.getFieldValue('dcmotorportsss');
		   //Blockly.Arduino.definitions_['md_dcmotorinit'] = 'Emakefun_Servo *servo'+dropdown_dcmotorports+' = mMotorDriver.getMotor('+dropdown_dcmotorports+');';
		return 'DCmotor_'+dropdown_dcmotorportsss+'->run(BRAKE);\n';
   }
   	//编码电机初始化
       Blockly.Arduino['md_enmotorinit'] = function(block) {
		   var dropdown_enmotorports = this.getFieldValue('enmotorports');
		   Blockly.Arduino.definitions_['md_enmotorinit_'+dropdown_enmotorports+''] = 'Emakefun_EncoderMotor *encodermotor_'+dropdown_enmotorports+' = mMotorDriver.getEncoderMotor('+dropdown_enmotorports+');';
		return 'mMotorDriver.begin(50);\n';
   }
   	//编码电机方向速度
       Blockly.Arduino['md_enmotorspeed'] = function(block) {
		   var dropdown_enmotorportss = this.getFieldValue('enmotorportss');
		   var dropdown_enmotordirections = this.getFieldValue('enmotordirections');
		   var value_enmotorspeeds = Blockly.Arduino.valueToCode(this, 'enmotorspeeds', Blockly.Arduino.ORDER_ATOMIC);
		   //Blockly.Arduino.definitions_['md_dcmotorinit'] = 'Emakefun_Servo *servo'+dropdown_dcmotorports+' = mMotorDriver.getMotor('+dropdown_dcmotorports+');';
		return 'encodermotor_'+dropdown_enmotorportss+'->setSpeed('+value_enmotorspeeds+');\nencodermotor_'+dropdown_enmotorportss+'->run('+dropdown_enmotordirections+');\n';
   }
   //停止编码电机
     Blockly.Arduino['md_stopenmotor'] = function(block) {
		   var dropdown_enmotorportsss = this.getFieldValue('enmotorportsss');
		   //Blockly.Arduino.definitions_['md_dcmotorinit'] = 'Emakefun_Servo *servo'+dropdown_dcmotorports+' = mMotorDriver.getMotor('+dropdown_dcmotorports+');';
		return 'encodermotor_'+dropdown_enmotorportsss+'->run(BRAKE);\n';
   }
   //步进电机初始化，每转步数
    Blockly.Arduino['md_stepmotorinit'] = function(block) {
		   var dropdown_stepmotorports = this.getFieldValue('stepmotorports');
		   var value_stepsperround = Blockly.Arduino.valueToCode(this, 'stepsperround', Blockly.Arduino.ORDER_ATOMIC);
		   Blockly.Arduino.definitions_['md_stepmotorinit_'+dropdown_stepmotorports+''] = 'Emakefun_StepperMotor *steppermotor_'+dropdown_stepmotorports+' = mMotorDriver.getStepper('+value_stepsperround+','+dropdown_stepmotorports+');';
		return 'mMotorDriver.begin();\n';
   }
    //步电机速度步数
    Blockly.Arduino['md_stepmotorspeed'] = function(block) {
		   var dropdown_stepmotorportsS = this.getFieldValue('stepmotorportsS');
		   var dropdown_stepmotordirections = this.getFieldValue('stepmotordirections');
		   var value_totalstep = Blockly.Arduino.valueToCode(this, 'totalstep', Blockly.Arduino.ORDER_ATOMIC);
		   var value_stepmotorspeed = Blockly.Arduino.valueToCode(this, 'stepmotorspeed', Blockly.Arduino.ORDER_ATOMIC);
		   //Blockly.Arduino.definitions_['md_stepmotorinit'] = 'Emakefun_StepperMotor *steppermotor_'+dropdown_stepmotorports+' = mMotorDriver.getStepper('+value_stepsperround+','+dropdown_stepmotorports+');';
		return 'steppermotor_'+dropdown_stepmotorportsS+'->setSpeed('+value_stepmotorspeed+');\nsteppermotor_'+dropdown_stepmotorportsS+'->step('+value_totalstep+','+dropdown_stepmotordirections+');\n';
   }
   //停止步进电机
    Blockly.Arduino['md_stopstepper'] = function(block) {
		   var dropdown_stepmotorportsSs = this.getFieldValue('stepmotorportsSs');
		   //Blockly.Arduino.definitions_['md_stepmotorinit'] = 'Emakefun_StepperMotor *steppermotor_'+dropdown_stepmotorports+' = mMotorDriver.getStepper('+value_stepsperround+','+dropdown_stepmotorports+');';
		return 'steppermotor_'+dropdown_stepmotorportsSs+'->release();\n';
   }
   //rgb初始化
      Blockly.Arduino['md_rgbinit'] = function(block) {
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		   Blockly.Arduino.definitions_['include_md_rgbinit'] = '#include <RGBLed.h>';
		   Blockly.Arduino.definitions_['md_rgbinit'] = 'RGBLed *rgb = mMotorDriver.getSensor(E_RGB);';
		return '';
   }
   //rgb颜色
    Blockly.Arduino['md_rgbcolor'] = function(block) {
		   var dropdown_RGBPOSITION = this.getFieldValue('RGBPOSITION');
		   var dropdown_RGBCOLOR = this.getFieldValue('RGBCOLOR');
            var value_rgbbrightness = Blockly.Arduino.valueToCode(this, 'rgbbrightness', Blockly.Arduino.ORDER_ATOMIC);
            var dropdown_color = block.childBlocks_[0].colour_; 
            var color  = dropdown_color.replace("#", "0x");
            // var result = dropdown_Serials9.colorRgb();
		  return 'rgb->setBrightness('+value_rgbbrightness+');\n rgb->SetRgbColor(' + dropdown_RGBPOSITION + ', ' + color + ');\n';
   }
   //rgb颜色 2
    Blockly.Arduino['md_rgbcolor2'] = function(block) {
		 Blockly.Arduino.definitions_['include_md_rgbcolor2'] = '#include <RGBLed.h>';
		  Blockly.Arduino.definitions_['md_rgbcolor2'] = 'RGBLed rgbled_A3(A1, 2);';
		 var dropdown_RGBPOSITION = this.getFieldValue('RGBPOSITION');
		   var value_red = Blockly.Arduino.valueToCode(this, 'red', Blockly.Arduino.ORDER_ATOMIC);
		   var value_green = Blockly.Arduino.valueToCode(this, 'green', Blockly.Arduino.ORDER_ATOMIC);
		   var value_blue = Blockly.Arduino.valueToCode(this, 'blue', Blockly.Arduino.ORDER_ATOMIC);
		    var value_rgbbrightness2 = Blockly.Arduino.valueToCode(this, 'rgbbrightness2', Blockly.Arduino.ORDER_ATOMIC);
		  return 'rgb->setBrightness('+value_rgbbrightness2+');\nrgbled_A3.setColor('+dropdown_RGBPOSITION+', '+value_red+','+value_green+','+value_blue+');\nrgbled_A3.show();\n';
   }
   //红外接收初始化
     Blockly.Arduino['md_irinit'] = function(block) {
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		   Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Keymap.h>\n#include<IRremote.h>';
		   Blockly.Arduino.definitions_['md_irinit'] = 'IRremote *ir = mMotorDriver.getSensor(E_IR);';
		return '';
   }
   //红外是否按下
   Blockly.Arduino.md_isirremote = function(){
  var code = 'irKeyCode = ir->getCode()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};
   //EMAKEFUN红外遥控器按下
    Blockly.Arduino['md_irpress'] = function(block) {
		   var dropdown_irpress = this.getFieldValue('irpress');
		   //var dropdown_RGBCOLOR = this.getFieldValue('RGBCOLOR');
		   Blockly.Arduino.definitions_['md_irpress'] = 'byte irKeyCode;';
		   var code = '(E_EM_IR_KEYCODE)ir->getIrKey(irKeyCode,2) == '+dropdown_irpress+''
		  return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   //普通红外遥控器按下
    Blockly.Arduino['md_irpress2'] = function(block) {
      var dropdown_irpress2 = this.getFieldValue('irpress2');
      //var dropdown_RGBCOLOR = this.getFieldValue('RGBCOLOR');
      Blockly.Arduino.definitions_['md_irpress2'] = 'byte irKeyCode;';
      var code = '(E_NORMAL_IR_KEYCODE)ir->getIrKey(irKeyCode) == '+dropdown_irpress2+''
     return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   //蜂鸣器初始化
    Blockly.Arduino['md_buzzerinit'] = function(block) {
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		   Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Buzzer.h>\n#include<Sounds.h>';
		   Blockly.Arduino.definitions_['md_buzzerinit'] = 'Emakefun_Sensor *buzzer = mMotorDriver.getSensor(E_SENSOR_MAX);';
		   return 'mMotorDriver.getSensor(E_BUZZER);\n';
   }
  //蜂鸣器播放
    Blockly.Arduino['md_buzzerplay'] = function(block) {
		   var dropdown_buzzerplays = this.getFieldValue('buzzerplays');
		   //var dropdown_RGBCOLOR = this.getFieldValue('RGBCOLOR');
		   //Blockly.Arduino.definitions_['md_irpress'] = 'byte irKeyCode;';
		  return 'buzzer->Sing('+dropdown_buzzerplays+');\n';
   }
   //超声波初始化
     Blockly.Arduino['md_ulinit'] = function(block) {
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		   //Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Buzzer.h>\n#include<Sounds.h>';
		   Blockly.Arduino.definitions_['md_ulinit'] = 'Emakefun_Sensor *ultrasonic = mMotorDriver.getSensor(E_SENSOR_MAX);';
		   return 'mMotorDriver.getSensor(E_ULTRASONIC_);\n';
   }
   //超声波读取距离
     Blockly.Arduino['md_ulplay'] = function(block) {
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		   //Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Buzzer.h>\n#include<Sounds.h>';
		   //Blockly.Arduino.definitions_['md_ulinit'] = 'Emakefun_Sensor *ultrasonic = mMotorDriver.getSensor(E_ULTRASONIC);';
		var code = 'ultrasonic->GetUltrasonicDistance()';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   //PS2初始化
    Blockly.Arduino['md_ps2init'] = function(block) {
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		   Blockly.Arduino.definitions_['include_md_ps2init'] = '#include <PS2X_lib.h>\n';
		   Blockly.Arduino.definitions_['md_ps2init'] = 'PS2X *ps2x;';
		   return 'ps2x = mMotorDriver.getSensor(E_PS2X);\n';
   }
   //ps2键被按下
    Blockly.Arduino['md_ps2pressed'] = function(block) {
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		   //Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Buzzer.h>\n#include<Sounds.h>';
		   //Blockly.Arduino.definitions_['md_ulinit'] = 'Emakefun_Sensor *ultrasonic = mMotorDriver.getSensor(E_ULTRASONIC);';
		var code = 'ps2x->ButtonDataByte()';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   //ps2按下
    Blockly.Arduino['md_ps2press'] = function(block) {
		   var dropdown_ps2buttons = this.getFieldValue('ps2buttons');
		   //Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Buzzer.h>\n#include<Sounds.h>';
		   //Blockly.Arduino.definitions_['md_ulinit'] = 'Emakefun_Sensor *ultrasonic = mMotorDriver.getSensor(E_ULTRASONIC);';
		var code = 'ps2x->Button('+dropdown_ps2buttons+')';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   //ps2获取摇杆值
    Blockly.Arduino['md_ps2getrockervalue'] = function(block) {
		   var dropdown_ps2rockers = this.getFieldValue('ps2rockers');
		   //Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Buzzer.h>\n#include<Sounds.h>';
		   //Blockly.Arduino.definitions_['md_ulinit'] = 'Emakefun_Sensor *ultrasonic = mMotorDriver.getSensor(E_ULTRASONIC);';
		var code = 'ps2x->Analog('+dropdown_ps2rockers+')';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
      //ps2获取摇杆角度
    Blockly.Arduino['md_ps2getrockervalue2'] = function(block) {
		   var dropdown_ps2rockerss = this.getFieldValue('ps2rockerss');
		   //Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Buzzer.h>\n#include<Sounds.h>';
		   //Blockly.Arduino.definitions_['md_ulinit'] = 'Emakefun_Sensor *ultrasonic = mMotorDriver.getSensor(E_ULTRASONIC);';
		var code = 'ps2x->'+dropdown_ps2rockerss+'';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   //读取ps2状态
     Blockly.Arduino['ps2status'] = function(block) {
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		   //Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Buzzer.h>\n#include<Sounds.h>';
		  //Blockly.Arduino.definitions_['md_ulinit'] = 'Emakefun_Sensor *ultrasonic = mMotorDriver.getSensor(E_ULTRASONIC);';
		return 'static int vibrate = 0;\nbyte PSS_X = 0,PSS_Y = 0;\nps2x->read_gamepad(false, vibrate);\n';
   }
  /* //nrf初始化
     Blockly.Arduino['md_nrfinits'] = function(block) {
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		   Blockly.Arduino.definitions_['include_md_ps2init'] = '#include <nRF24L01.h>\n';
		   Blockly.Arduino.definitions_['md_ps2init'] = 'Emakefun_Sensor *nrf24l01= mMotorDriver.getSensor(E_SENSOR_NRF24L01);';
		return '';
   }
  //nrf发送数据
        Blockly.Arduino['md_nrfsend'] = function(block) {
			var value_address = Blockly.Arduino.valueToCode(this, 'address', Blockly.Arduino.ORDER_ATOMIC);
			var value_data = Blockly.Arduino.valueToCode(this, 'data', Blockly.Arduino.ORDER_ATOMIC);
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		return 'nrf24l01->sendNrf24l01((char *)"'+value_address.replace(/'/g, '')+'",'+value_data+');\n';
   }
   //nrf接收数据
 Blockly.Arduino['md_nrfreceive'] = function(block) {
			var value_address2 = Blockly.Arduino.valueToCode(this, 'address2', Blockly.Arduino.ORDER_ATOMIC);
			//var value_data = Blockly.Arduino.valueToCode(this, 'data', Blockly.Arduino.ORDER_ATOMIC);
		   //var dropdown_enmotorports = this.getFieldValue('enmotorports');
		return 'int NrfData;\nNrfData = nrf24l01->GetNrf24L01((char *)"'+value_address2.replace(/'/g, '')+'");\n';
   }
  //NRF数据不为空
 Blockly.Arduino['md_nrfdataisnotnull'] = function(block) {
		   //var dropdown_ps2rockers = this.getFieldValue('ps2rockers');
		   //Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Buzzer.h>\n#include<Sounds.h>';
		   //Blockly.Arduino.definitions_['md_ulinit'] = 'Emakefun_Sensor *ultrasonic = mMotorDriver.getSensor(E_ULTRASONIC);';
		var code = 'NrfData != NULL';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
//NRF数据
 Blockly.Arduino['md_nrfdata'] = function(block) {
		   //var dropdown_ps2rockers = this.getFieldValue('ps2rockers');
		   //Blockly.Arduino.definitions_['include_md_irinit'] = '#include <Buzzer.h>\n#include<Sounds.h>';
		   //Blockly.Arduino.definitions_['md_ulinit'] = 'Emakefun_Sensor *ultrasonic = mMotorDriver.getSensor(E_ULTRASONIC);';
		var code = 'NrfData';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }*/
//四驱车初始化



//发送数组
Blockly.Arduino.md_nrf24l01send = function(){
 var value_address = Blockly.Arduino.valueToCode(this,'address',Blockly.Arduino.ORDER_ATOMIC);
 var value_channel = Blockly.Arduino.valueToCode(this,'channel',Blockly.Arduino.ORDER_ATOMIC);
 var value_arr = Blockly.Arduino.valueToCode(this,'arr',Blockly.Arduino.ORDER_ATOMIC);
    //Blockly.Arduino.definitions_['md_nrf24l01send123'] = ''
	Blockly.Arduino.definitions_['md_nrf24l01send'] = '#include<nRF24L01.h>\nNrf24l Mirf = Nrf24l(10, 9);\n'
	//Blockly.Arduino.definitions_['md_nrf24l01send2'] =''
 var code = 'Serial.begin(9600);\n  Mirf.init();\n  Mirf.setRADDR('+value_address.replace(/'/g, '"')+');\n  Mirf.payload = sizeof('+value_arr.replace(/'/g, '')+');\n  Mirf.channel = '+value_channel+';\n  Mirf.config();\n';
 return code;
};
//接收数组
Blockly.Arduino.md_nrf24l01rec = function(){

 var value_address2 = Blockly.Arduino.valueToCode(this,'address2',Blockly.Arduino.ORDER_ATOMIC);
 var value_channel2 = Blockly.Arduino.valueToCode(this,'channel2',Blockly.Arduino.ORDER_ATOMIC);
 var value_arr2 = Blockly.Arduino.valueToCode(this,'arr2',Blockly.Arduino.ORDER_ATOMIC);
    //Blockly.Arduino.definitions_['md_nrf24l01send123'] = ''
	Blockly.Arduino.definitions_['md_nrf24l01rec'] = '#include<nRF24L01.h>\nNrf24l Mirf = Nrf24l(10, 9);\n'
	//Blockly.Arduino.definitions_['md_nrf24l01send2'] =''
 var code = 'Serial.begin(9600);\n  Mirf.init();\n  Mirf.setRADDR('+value_address2.replace(/'/g, '"')+');\n  Mirf.payload = sizeof('+value_arr2.replace(/'/g, '')+');\n  Mirf.channel = '+value_channel2+';\n  Mirf.config();\n';
 return code;
};
//创建数组
Blockly.Arduino.md_makearr = function(){
 var value_arrname = Blockly.Arduino.valueToCode(this,'arrname',Blockly.Arduino.ORDER_ATOMIC);
 var value_arrnum = Blockly.Arduino.valueToCode(this,'arrnum',Blockly.Arduino.ORDER_ATOMIC);
 //var value_arr = Blockly.Arduino.valueToCode(this,'arr',Blockly.Arduino.ORDER_ATOMIC);
 var dropdown_srrstyle = this.getFieldValue('srrstyle');
    //Blockly.Arduino.definitions_['md_nrf24l01send123'] = ''
	Blockly.Arduino.definitions_['md_makearr'] = ''+dropdown_srrstyle+' '+value_arrname.replace(/'/g, '')+' ['+value_arrnum.replace(/'/g, '')+'];\n'
	//Blockly.Arduino.definitions_['md_nrf24l01send2'] =''
 var code = '';
 return code;
};
//数组的第几项
Blockly.Arduino.md_setarr = function(){
 var value_arrname2 = Blockly.Arduino.valueToCode(this,'arrname2',Blockly.Arduino.ORDER_ATOMIC);
 var value_arrno2 = Blockly.Arduino.valueToCode(this,'arrno2',Blockly.Arduino.ORDER_ATOMIC);
 //var value_arr = Blockly.Arduino.valueToCode(this,'arr',Blockly.Arduino.ORDER_ATOMIC);
 //var dropdown_srrstyle = this.getFieldValue('srrstyle');
    //Blockly.Arduino.definitions_['md_nrf24l01send123'] = ''
	//Blockly.Arduino.definitions_['md_nrf24l01send1'] = ''+dropdown_srrstyle+' '+value_arrname.replace(/'/g, '')+' ['+value_arrnum.replace(/'/g, '')+'];\n'
	//Blockly.Arduino.definitions_['md_nrf24l01send2'] =''
 var code = ''+value_arrname2.replace(/'/g, '')+'[('+value_arrno2.replace(/'/g, '')+'-1)]';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
};
//数组的第几项赋值为
Blockly.Arduino.md_setarr2 = function(){
 var value_arrname22 = Blockly.Arduino.valueToCode(this,'arrname22',Blockly.Arduino.ORDER_ATOMIC);
 var value_arrno22 = Blockly.Arduino.valueToCode(this,'arrno22',Blockly.Arduino.ORDER_ATOMIC);
 var value_arrvalue = Blockly.Arduino.valueToCode(this,'arrvalue',Blockly.Arduino.ORDER_ATOMIC);
 //var dropdown_srrstyle = this.getFieldValue('srrstyle');
    //Blockly.Arduino.definitions_['md_nrf24l01send123'] = ''
	//Blockly.Arduino.definitions_['md_nrf24l01send1'] = ''+dropdown_srrstyle+' '+value_arrname.replace(/'/g, '')+' ['+value_arrnum.replace(/'/g, '')+'];\n'
	//Blockly.Arduino.definitions_['md_nrf24l01send2'] =''
 var code = ''+value_arrname22.replace(/'/g, '')+'[('+value_arrno22.replace(/'/g, '')+'-1)] = '+value_arrvalue.replace(/'/g, '')+';\n';
 return code;
};
//nrf24l01发送数据
Blockly.Arduino.md_nrf24l01send2 = function(){
 var value_nrfdata2 = Blockly.Arduino.valueToCode(this,'nrfdata2',Blockly.Arduino.ORDER_ATOMIC);
 //var value_arrno22 = Blockly.Arduino.valueToCode(this,'arrno22',Blockly.Arduino.ORDER_ATOMIC);
 //var value_arrvalue = Blockly.Arduino.valueToCode(this,'arrvalue',Blockly.Arduino.ORDER_ATOMIC);
 //var dropdown_srrstyle = this.getFieldValue('srrstyle');
    //Blockly.Arduino.definitions_['md_nrf24l01send123'] = ''
	//Blockly.Arduino.definitions_['md_nrf24l01send1'] = ''+dropdown_srrstyle+' '+value_arrname.replace(/'/g, '')+' ['+value_arrnum.replace(/'/g, '')+'];\n'
	//Blockly.Arduino.definitions_['md_nrf24l01send2'] =''
 var code = 'Mirf.send('+value_nrfdata2.replace(/'/g, '')+');\n';
 return code;
};
//是否发送成功？
Blockly.Arduino.md_nrf24l01send2isok = function(){
 var code = 'Mirf.isSending()';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
};
//是否接收到数据
Blockly.Arduino.md_nrf24l01send2isrec = function(){
 var code = 'Mirf.dataReady()';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
};
//nrf接收数据
Blockly.Arduino.md_nrf24l01rec2 = function(){
 var value_nrfdata3 = Blockly.Arduino.valueToCode(this,'nrfdata3',Blockly.Arduino.ORDER_ATOMIC);
 var code = 'Mirf.getData('+value_nrfdata3.replace(/'/g, '')+');\n';
 return code;
};
//四驱车初始化
Blockly.Arduino.md_m4init = function () {
    var dropdown_M4Ports1 = this.getFieldValue('M4Ports1');
    var dropdown_M4Ports2 = this.getFieldValue('M4Ports2');
    var dropdown_M4Ports3 = this.getFieldValue('M4Ports3');
    var dropdown_M4Ports4 = this.getFieldValue('M4Ports4');
    Blockly.Arduino.definitions_['md_m4init'] = '#include<Emakefun_MotorDriver.h>\n'
    Blockly.Arduino.definitions_['md_m4init2'] ='Emakefun_DCMotor *DCmotor_'+dropdown_M4Ports1+' = mMotorDriver.getMotor(' + dropdown_M4Ports1 + ');\n    Emakefun_DCMotor *DCmotor_'+dropdown_M4Ports2+' = mMotorDriver.getMotor(' + dropdown_M4Ports2 + ');\n    Emakefun_DCMotor *DCmotor_'+dropdown_M4Ports3+' = mMotorDriver.getMotor(' + dropdown_M4Ports3 + ');\n    Emakefun_DCMotor *DCmotor_'+dropdown_M4Ports4+' = mMotorDriver.getMotor(' + dropdown_M4Ports4 + ');'
    var code = 'mMotorDriver.begin(50);\n'
    return code;
};
//前进
Blockly.Arduino.md_m4goforward = function () {
    var value_m4speed1 = Blockly.Arduino.valueToCode(this, 'm4speed1', Blockly.Arduino.ORDER_ATOMIC);
    var code = '    LeftFoward->setSpeed(' + value_m4speed1 + ');\n    RightFoward->setSpeed(' + value_m4speed1 + ');\n    LeftBackward->setSpeed(' + value_m4speed1 + ');\n    RightBackward->setSpeed(' + value_m4speed1 + ');\n    LeftFoward->run(FORWARD);\n    RightFoward->run(FORWARD);\n    LeftBackward->run(FORWARD);\n    RightBackward->run(FORWARD);\n';
    return code;
};
//后退
Blockly.Arduino.md_m4backforward = function () {
    var value_m4speed2 = Blockly.Arduino.valueToCode(this, 'm4speed2', Blockly.Arduino.ORDER_ATOMIC);
    var code = '    LeftFoward->setSpeed(' + value_m4speed2 + ');\n    RightFoward->setSpeed(' + value_m4speed2 + ');\n    LeftBackward->setSpeed(' + value_m4speed2 + ');\n    RightBackward->setSpeed(' + value_m4speed2 + ');\n    LeftFoward->run(BACKWARD);\n    RightFoward->run(BACKWARD);\n    LeftBackward->run(BACKWARD);\n    RightBackward->run(BACKWARD);\n';
    return code;
};
//左转
Blockly.Arduino.md_m4turnleft = function () {
    var value_m4speed3 = Blockly.Arduino.valueToCode(this, 'm4speed3', Blockly.Arduino.ORDER_ATOMIC);
    var code = '    LeftFoward->setSpeed(' + value_m4speed3 + '/2);\n    RightFoward->setSpeed(' + value_m4speed3 + ');\n    LeftBackward->setSpeed(' + value_m4speed3 + '/2);\n    RightBackward->setSpeed(' + value_m4speed3 + ');\n    LeftFoward->run(FORWARD);\n    RightFoward->run(FORWARD);\n    LeftBackward->run(FORWARD);\n    RightBackward->run(FORWARD);\n';
    return code;
};
//右转
Blockly.Arduino.md_m4turnright = function () {
    var value_m4speed4 = Blockly.Arduino.valueToCode(this, 'm4speed4', Blockly.Arduino.ORDER_ATOMIC);
    var code = '    LeftFoward->setSpeed(' + value_m4speed4 + ');\n    RightFoward->setSpeed(' + value_m4speed4 + '/2);\n    LeftBackward->setSpeed(' + value_m4speed4 + ');\n    RightBackward->setSpeed(' + value_m4speed4 + '/2);\n    LeftFoward->run(FORWARD);\n    RightFoward->run(FORWARD);\n    LeftBackward->run(FORWARD);\n    RightBackward->run(FORWARD);\n';
    return code;
};
//左自旋
Blockly.Arduino.md_m4turnlefts = function () {
    var value_m4speed33 = Blockly.Arduino.valueToCode(this, 'm4speed33', Blockly.Arduino.ORDER_ATOMIC);
    var code = '    LeftFoward->setSpeed(' + value_m4speed33 + ');\n    RightFoward->setSpeed(' + value_m4speed33 + ');\n    LeftBackward->setSpeed(' + value_m4speed33 + ');\n    RightBackward->setSpeed(' + value_m4speed33 + ');\n    LeftFoward->run(BACKWARD);\n    RightFoward->run(FORWARD);\n    LeftBackward->run(BACKWARD);\n    RightBackward->run(FORWARD);\n';
    return code;
};
//右自旋
Blockly.Arduino.md_m4turnrights = function () {
    var value_m4speed44 = Blockly.Arduino.valueToCode(this, 'm4speed44', Blockly.Arduino.ORDER_ATOMIC);
    var code = '    LeftFoward->setSpeed(' + value_m4speed44 + ');\n    RightFoward->setSpeed(' + value_m4speed44 + ');\n    LeftBackward->setSpeed(' + value_m4speed44 + ');\n    RightBackward->setSpeed(' + value_m4speed44 + ');\n    LeftFoward->run(FORWARD);\n    RightFoward->run(BACKWARD);\n    LeftBackward->run(FORWARD);\n    RightBackward->run(BACKWARD);\n';
    return code;
};
//行驶角度
Blockly.Arduino.md_m4godegree = function () {
    var value_m4godegree = Blockly.Arduino.valueToCode(this, 'm4godegree', Blockly.Arduino.ORDER_ATOMIC);
    var value_m4gospeed = Blockly.Arduino.valueToCode(this, 'm4gospeed', Blockly.Arduino.ORDER_ATOMIC);
    if (value_m4godegree >= 0 && value_m4godegree <= 90) {
        var code = '    LeftFoward->setSpeed(' + value_m4gospeed + ');\n    RightFoward->setSpeed(' + value_m4gospeed + ' * ((float)(' + value_m4godegree + ') / 90));\n    LeftBackward->setSpeed(' + value_m4gospeed + ');\n    RightBackward->setSpeed(' + value_m4gospeed + ' * ((float)(' + value_m4godegree + ') / 90));\n    LeftFoward->run(FORWARD);\n    RightFoward->run(FORWARD);\n    LeftBackward->run(FORWARD);\n    RightBackward->run(FORWARD);\n';
    } else if (value_m4godegree > 90 && value_m4godegree <= 180) {
        var code = '    LeftFoward->setSpeed(' + value_m4gospeed + ' * (float)(180 - ' + value_m4godegree + ') / 90);\n    RightFoward->setSpeed(' + value_m4gospeed + ');\n    LeftBackward->setSpeed(' + value_m4gospeed + ' * (float)(180 - ' + value_m4godegree + ') / 90);\n    RightBackward->setSpeed(' + value_m4gospeed + ');\n    LeftFoward->run(FORWARD);\n    RightFoward->run(FORWARD);\n    LeftBackward->run(FORWARD);\n    RightBackward->run(FORWARD);\n';
    } else if (value_m4godegree > 180 && value_m4godegree <= 270) {
        var code = '    LeftFoward->setSpeed(' + value_m4gospeed + ' * ((float)(' + value_m4godegree + ' - 180) / 90));\n    RightFoward->setSpeed(' + value_m4gospeed + ');\n    LeftBackward->setSpeed(' + value_m4gospeed + ' * ((float)(' + value_m4godegree + ' - 180) / 90));\n    RightBackward->setSpeed(' + value_m4gospeed + ');\n    LeftFoward->run(BACKWARD);\n    RightFoward->run(BACKWARD);\n    LeftBackward->run(BACKWARD);\n    RightBackward->run(BACKWARD);\n';
    } else if (value_m4godegree > 270 && value_m4godegree <= 360) {
        var code = '    LeftFoward->setSpeed(' + value_m4gospeed + ');\n    RightFoward->setSpeed(' + value_m4gospeed + ' * (float)(360 - ' + value_m4godegree + ') / 90);\n    LeftBackward->setSpeed(' + value_m4gospeed + ');\n    RightBackward->setSpeed(' + value_m4gospeed + ' * (float)(360 - ' + value_m4godegree + ') / 90);\n    LeftFoward->run(BACKWARD);\n    RightFoward->run(BACKWARD);\n    LeftBackward->run(BACKWARD);\n    RightBackward->run(BACKWARD);\n';
    } else {
        var code = '    LeftFoward->run(BRAKE);\n    RightFoward->run(BRAKE);\n    LeftBackward->run(BRAKE);\n    RightBackward->run(BRAKE);\n';
    }

    return code;
};
//停止
Blockly.Arduino.md_m4stop = function () {
    var code = '    LeftFoward->run(BRAKE);\n    RightFoward->run(BRAKE);\n    LeftBackward->run(BRAKE);\n    RightBackward->run(BRAKE);\n';
    return code;
};
 //蓝牙初始化
Blockly.Arduino.md_bluetoothinit = function () {
    //var dropdown_DCPorts = this.getFieldValue('DCPorts');
    Blockly.Arduino.definitions_['md_bluetoothinit'] = '#include<ProtocolParser.h>\n#include<BluetoothHandle.h>\nProtocolParser *mProtocol = new ProtocolParser();\n'
    var code = ''
    return code;
};
   //接受蓝牙数据
Blockly.Arduino.md_receivedata = function () {
    var code = 'mProtocol->RecevData();\n';
    return code;
};
    //接受到了蓝牙数据
Blockly.Arduino.md_receiveddata = function () {
    var code = 'mProtocol->ParserPackage()';
    return [code, Blockly.Arduino.ORDER_ATOMIC]; //当code为表达式时用此返回方式
};

Blockly.Arduino.md_bluetoothKeyPress = function () {
    var dropdown_BLUETOOTHKEY = this.getFieldValue('BLUETOOTHKEY');
    var code = 'mProtocol->GetBluetoothButton()==' + dropdown_BLUETOOTHKEY + '';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.md_bluetoothstyle = function () {
    var dropdown_BLUETOOTHSTYLE = this.getFieldValue('BLUETOOTHSTYLE');
    var code = 'mProtocol->GetRobotControlFun()==' + dropdown_BLUETOOTHSTYLE + '';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.md_bluetoothgetdegree = function () {
    var code = 'mProtocol->GetRobotDegree()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};
//nrf初始化
Blockly.Arduino.md_nrfinit = function () {
    //Blockly.Arduino.definitions_['md_mdinit'] = 'Emakefun_MotorDriver mMotorDriver = Emakefun_MotorDriver();';
    Blockly.Arduino.definitions_['md_nrfinit'] = '#include<ProtocolParser.h>\n#include<BluetoothHandle.h>\n#include<nRF24L01.h>\nNrf24l  *Nrf24L01 = mMotorDriver.getSensor(E_NRF24L01);\nProtocolParser *mProtocol = new ProtocolParser();\n';
    var value_nrfadd = Blockly.Arduino.valueToCode(this, 'nrfadd', Blockly.Arduino.ORDER_ATOMIC);
    //Blockly.Arduino.setups_['md_nrfinit'] = ''
    var code = 'Nrf24L01->setRADDR(' + value_nrfadd.replace(/'/g, '"') + ');\n';
    return code;
};
//nrf数据已准备
Blockly.Arduino.md_nrfdataready = function () {
    var code = 'Nrf24L01->dataReady()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};
//定义数据数组
Blockly.Arduino.md_nrfvalue = function () {
    //var value_nrfadd = Blockly.Arduino.valueToCode(this, 'nrfadd', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'byte value[12] = {0};\nbool godata = false;\n';
    return code;
};
//nrf接收数据
Blockly.Arduino.md_nrfgetdata = function () {
    //var value_nrfadd = Blockly.Arduino.valueToCode(this, 'nrfadd', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'Nrf24L01->getData(value);\nmProtocol->RecevData(value, 12);\n';
    return code;
};
//获取到nrf数据包？
Blockly.Arduino.md_nrfgetpackage = function () {
    var code = 'mProtocol->ParserPackage()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//创建nrf数据类型对象
Blockly.Arduino.md_nrfnewob = function () {
    //var value_nrfadd = Blockly.Arduino.valueToCode(this, 'nrfadd', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'E_CONTOROL_FUNC fun = mProtocol->GetRobotControlFun();\n';
    return code;
};
//nrf数据类型是？下拉框
Blockly.Arduino.md_nrfdatafun = function () {
    var dropdown_datafun = this.getFieldValue('datafun');
    var code = 'fun == ' + dropdown_datafun + '';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};
//无线手柄按键按下？下拉框
Blockly.Arduino.md_nrfbuttons = function () {
    var dropdown_nrfbuttons = this.getFieldValue('nrfbuttons');
    var code = 'mProtocol->GetBluetoothButton() == ' + dropdown_nrfbuttons + '';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};
//nrf获取的角度
Blockly.Arduino.md_nrfgetdegree = function () {
    //var value_nrfadd = Blockly.Arduino.valueToCode(this, 'nrfadd', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'mProtocol->GetRobotDegree()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};
//nrf获取的速度
Blockly.Arduino.md_nrfgetspeed = function () {
    //var value_nrfadd = Blockly.Arduino.valueToCode(this, 'nrfadd', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'mProtocol->GetRobotSpeed()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};



























   //lcd配置地址
   Blockly.Arduino['motorDriverBoard_20lcdinit'] = function(block) {
    var value_lcdinits = Blockly.Arduino.valueToCode(this, 'lcdinits', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_lcdinit'] = '#include <LiquidCrystal_I2C.h>\n#include <Wire.h>';
    Blockly.Arduino.definitions_['lcdinit'] = 'LiquidCrystal_I2C mylcd('+value_lcdinits.replace(/'/g, "")+',16,2);';
    //Blockly.Arduino.definitions_['setup_lcdinit'] = '';
    return  'mylcd.init();\nmylcd.backlight();\n';
  }
  //lcd光标
  Blockly.Arduino['motorDriverBoard_20lcdposition'] = function(block) {
    var value_col = Blockly.Arduino.valueToCode(this, 'col', Blockly.Arduino.ORDER_ATOMIC);
    var value_row = Blockly.Arduino.valueToCode(this, 'row', Blockly.Arduino.ORDER_ATOMIC);
    return  'mylcd.setCursor('+value_col+'-1, '+value_row+'-1);\n';
  }
  //lcd打印
  Blockly.Arduino['motorDriverBoard_20lcdprint'] = function(block) {
  var value_lcdprints = Blockly.Arduino.valueToCode(this, 'lcdprints', Blockly.Arduino.ORDER_ATOMIC);
  return  'mylcd.print('+value_lcdprints.replace(/'/g, '"')+');\n';
  }
  //lcd清屏
  Blockly.Arduino['motorDriverBoard_20lcdclear'] = function(block) {
  return  'mylcd.clear();\n';
  }
//LED
Blockly.Arduino['motorDriverBoard_led'] = function(block) {
    var dropdown_led = this.getFieldValue('led');
    var dropdown_status = this.getFieldValue('status');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_led'] = 'PH20Port led('+dropdown_led + ");\n";
      // Blockly.Arduino.definitions_['setup_ph_led'] = 'pinMode('+dropdown_led+', OUTPUT);\n';
      //Blockly.Arduino.definitions_['ph_led'] = '';
      var code = dropdown_status=="1"?"led.dWrite1(HIGH);":"led.dWrite1(LOW);";
return code;
}

//激光放射器
Blockly.Arduino['motorDriverBoard_laser'] = function(block) {
  var dropdown_laser = this.getFieldValue('laser');
  var dropdown_laserStatus = this.getFieldValue('laserStatus');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_laser'] = 'PH20Port laser('+dropdown_laser + ");\n";
    // Blockly.Arduino.definitions_['setup_ph_led'] = 'pinMode('+dropdown_led+', OUTPUT);\n';
    //Blockly.Arduino.definitions_['ph_led'] = '';
  var code = dropdown_laserStatus=="1"?"laser.dWrite1(HIGH);\n":"laser.dWrite1(LOW);\n";
  return code;
}
//声音传感器
Blockly.Arduino['motorDriverBoard_vioce'] = function(block) {
    var dropdown_phvioce = this.getFieldValue('phvioce');
    var vioceValue = this.getFieldValue('vioceValue');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_vioce'] = 'PH20Port vioce('+dropdown_phvioce + ");\n";
    var code = vioceValue;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//光敏传感器
Blockly.Arduino['motorDriverBoard_light'] = function(block) {
    var dropdown_phlight = this.getFieldValue('phlight');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_light'] = 'PH20Port light('+dropdown_phlight + ");\n";
    	//Blockly.Arduino.definitions_['ph_led'] = '';
      var code = 'light.aRead1()';
      	return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//触摸传感器
Blockly.Arduino['motorDriverBoard_touch'] = function(block) {
    var dropdown_phtouch = this.getFieldValue('phtouch');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_touch'] = 'PH20Port touch('+dropdown_phtouch + ");\n";
    // Blockly.Arduino.definitions_['setup_ph_touch'] = 'pinMode('+dropdown_phtouch+', INPUT);\n';
    	//Blockly.Arduino.definitions_['ph_led'] = '';
      var code = 'touch.dpRead1()==HIGH';
      	return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//按键传感器
Blockly.Arduino['motorDriverBoard_button'] = function(block) {
    var dropdown_phbutton = this.getFieldValue('phbutton');
    var dropdown_isButton = this.getFieldValue('isButton');
    Blockly.Arduino.definitions_['include_button'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['initButton'] = 'PH20Port button('+ dropdown_phbutton +');\n'
    // Blockly.Arduino.definitions_['setup_ph_button'] = 'pinMode('+dropdown_phbutton+', INPUT);\n';
      //Blockly.Arduino.definitions_['ph_led'] = '';
      var code = '';
      if(dropdown_isButton == 'down'){
        code = 'button.dpRead1() == LOW';
      }else if(dropdown_isButton == 'loosen'){
        code = 'button.dpRead1() == HIGH';
      }else{
        Blockly.Arduino.definitions_['initFlag'] = 'boolean flag;\n'
        Blockly.Arduino.definitions_['timer_run_button'] = 'for( int i = 0 ;i < 3000;i++ ){\n'+
          '   if(button.dpRead1() == LOW){\n'+
            '     delay(1);\n'+
            '      flag = true;\n'+
          '   }else{\n'+
            '     flag = false;\n'+
            '     delay(1);\n'+
            '     break;\n'+
            '   \n'+
            '   }\n'+
            '}\n';

        code = 'flag';
      } 
      	return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//热敏传感器
Blockly.Arduino['motorDriverBoard_thermosensitive'] = function(block) {
    var dropdown_thermosensitive = this.getFieldValue('thermosensitive');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_thermosensitive'] = 'PH20Port thermosensitive('+dropdown_thermosensitive + ");\n";
      // Blockly.Arduino.definitions_['setup_ph_thermosensitive'] = 'pinMode('+dropdown_thermosensitive+', INPUT);';
    var code = 'thermosensitive.aRead1()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//火焰传感器
Blockly.Arduino['motorDriverBoard_fire'] = function(block) {
    var dropdown_fire = this.getFieldValue('fire');
    var dropdown_fireValue = this.getFieldValue('fireValue');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_fire'] = 'PH20Port fire('+dropdown_fire + ");\n";
    var code = 'fire.'+dropdown_fireValue;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//避障传感器
Blockly.Arduino['motorDriverBoard_abstacle'] = function(block) {
  var dropdown_abstacle = this.getFieldValue('abstaclePort');
  var dropdown_abstacleValue = this.getFieldValue('abstacleValue');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_fire'] = 'PH20Port avoidance('+dropdown_abstacle + ");\n";
  var code = 'avoidance.'+dropdown_abstacleValue;
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//土壤湿度传感器
Blockly.Arduino['motorDriverBoard_soil'] = function(block) {
    var dropdown_soil = this.getFieldValue('soil');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_soil'] = 'PH20Port soil('+dropdown_soil + ");\n";
    var code = 'soil.aRead1()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//雨滴传感器
Blockly.Arduino['motorDriverBoard_rain'] = function(block) {
    var dropdown_rain = this.getFieldValue('rain');
    var dropdown_rainValue = this.getFieldValue('rainValue');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_rain'] = 'PH20Port rain('+dropdown_rain + ");\n";
    var code = 'rain.' + dropdown_rainValue;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//水深传感器
Blockly.Arduino['motorDriverBoard_waterdeep'] = function(block) {
    var dropdown_waterdeep = this.getFieldValue('waterdeep');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_waterdeep'] = 'PH20Port waterdeep('+dropdown_waterdeep + ");\n";
    var code = 'waterdeep.aRead1()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//碰撞传感器
Blockly.Arduino['motorDriverBoard_knockswitch'] = function(block) {
    var dropdown_knockswitch = this.getFieldValue('knockswitch');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_knockswitch'] = 'PH20Port knockSwitch('+dropdown_knockswitch + ");\n";
    	//Blockly.Arduino.definitions_['ph_led'] = '';
    var code = 'knockSwitch.dpRead1() == LOW';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//dht11
Blockly.Arduino['motorDriverBoard_dht11'] = function(block) {
    var dropdown_dht11 = this.getFieldValue('dht11');
    var dropdown_temhum = this.getFieldValue('temhum');
    Blockly.Arduino.definitions_['include_ph_dht11'] = '#include "dht11.h"\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_port'] = 'dht11 dht11;\n';
    Blockly.Arduino.definitions_['ph_port'] = 'PH20Port dhtPort(' + dropdown_dht11 + ");\n";
    Blockly.Arduino.definitions_['timer_run_read'] = ' dht11.read(dhtPort.pin1());\n';
    var code = '(float)dht11.'+ dropdown_temhum;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//温度传感器
Blockly.Arduino['motorDriverBoard_ds'] = function(block) {
    var dropdown_ds = this.getFieldValue('ds');
    Blockly.Arduino.definitions_['include_ph_ds'] = '#include "OneWire.h"\n#include "DallasTemperature.h"\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['setup_ph_ds'] = '  dallasTemperature.begin();\n';
    Blockly.Arduino.definitions_['ph_ds'] = 'PH20Port ds18b20(' + dropdown_ds + ');\nOneWire oneWire(ds18b20.pin1());\nDallasTemperature dallasTemperature(&oneWire);\n';
    var code = '';
    return code;
}

//温度传感器WWW
Blockly.Arduino['motorDriverBoard_lm35'] = function(block) {
  var dropdown_lm35 = this.getFieldValue('lm35');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['ph_portLm35'] = 'PH20Port lm35(' + dropdown_lm35 + ");\n";
  Blockly.Arduino.definitions_['include_ps_lm35Value'] = 'int val,lm35Data; \nfloat voltage = 0.0;\n';
  Blockly.Arduino.definitions_['timer_run_lm35'] = " val = lm35.aRead1();\n  voltage = ((float)val/1023);\n  voltage = voltage * 5;\n  lm35Data = voltage * 100;\n";
  // Blockly.Arduino.definitions_['ph_ds'] = 'PH20Port ds18b20(' + dropdown_ds + ');\nOneWire oneWire(ds18b20.pin1());\nDallasTemperature dallasTemperature(&oneWire);\n';
  var code = 'lm35Data';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//温度传感器获取温度  ph_lm35
Blockly.Arduino['motorDriverBoard_readds'] = function(block) {
  Blockly.Arduino.definitions_['timer_run_get'] = ' dallasTemperature.requestTemperatures(); \n';
  var code = 'dallasTemperature.getTempCByIndex(0)';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//RGB超声波初始化
Blockly.Arduino['motorDriverBoard_rgbUltrasonicInit'] = function(block) {
  var dropdown_ulPINS = this.getFieldValue('ph_ulPINS');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_rgbUltrasonic'] = '#include "RgbUltrasonic.h"\nPH20Port rgbUltrasonic('+ dropdown_ulPINS +');\nRgbUltrasonic mRUS04(rgbUltrasonic.pin1(), rgbUltrasonic.pin2());\n';
  var code = '';
  return code;
}

//RGB超声波设置颜色
Blockly.Arduino['motorDriverBoard_rgbultrasonicsetcolorstyle'] = function(block) {
  var rgbultrasonicposition = this.getFieldValue('ph_rgbultrasonicposition');
  var rgbultrasoniccolor = this.getFieldValue('ph_rgbultrasoniccolor');
  var rgbultrasoniccolorstyle = this.getFieldValue('ph_rgbultrasoniccolorstyle');
  var dropdown_color = block.childBlocks_[0].colour_; 
  dropdown_color = dropdown_color.replace("#","0x");
  // if(rgbultrasoniccolor == "RGB_RED") {
  //   rgbultrasoniccolor == "RGB_GREEN";
  // }else if(rgbultrasoniccolor == "RGB_GREEN") {
  //   rgbultrasoniccolor == "RGB_GREEN";
  // }
  var code = 'mRUS04.SetRgbEffect(' + rgbultrasonicposition+', ' + dropdown_color + ', ' + rgbultrasoniccolorstyle + ');\n  delay(1000);\n';
  return code;
}

//RGB超声波获取距离
Blockly.Arduino['motorDriverBoard_rgbultrasonicreaddistance'] = function(block) {
  var code = 'mRUS04.GetUltrasonicDistance()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}


// //温度传感器读取温度
// Blockly.Arduino['motorDriverBoard_readds'] = function(block) {
//       var code = 'sensors.getTempCByIndex(0)';
//       	return [code, Blockly.Arduino.ORDER_ATOMIC];
// }
//气体传感器
Blockly.Arduino['motorDriverBoard_air'] = function(block) {
    var dropdown_air = this.getFieldValue('air');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_air'] = 'PH20Port air('+ dropdown_air + ");\n";
    var dropdown_airValue = this.getFieldValue('airValue');
    var code = 'air.' + dropdown_airValue;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//人体热释电传感器
Blockly.Arduino['motorDriverBoard_body'] = function(block) {
    var dropdown_body = this.getFieldValue('body');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_body'] = 'PH20Port body('+ dropdown_body + ");\n";
    // Blockly.Arduino.definitions_['setup_ph_body'] = 'pinMode('+dropdown_body+', INPUT);';
    	//Blockly.Arduino.definitions_['ph_led'] = '';
    var code = 'body.dpRead1() == HIGH';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//循迹传感器
Blockly.Arduino['motorDriverBoard_track'] = function(block) {
    var dropdown_track = this.getFieldValue('track');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_track'] = 'PH20Port track('+ dropdown_track + ");\n";
      // Blockly.Arduino.definitions_['setup_ph_track'] = 'pinMode('+dropdown_track+', INPUT);';
    	//Blockly.Arduino.definitions_['ph_led'] = '';
      var code = 'track.dpRead1() == LOW';
      	return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//蜂鸣器播放声音选择声音
Blockly.Arduino['motorDriverBoard_buzzerplay2'] = function(block) {
  var dropdown_buzzerinitPort = this.getFieldValue('phBuzzerInitPort');
  var dropdown_songs = this.getFieldValue('ph_songs');
  Blockly.Arduino.definitions_['include_buzzerplay2'] = '#include <Buzzer.h>\n#include <Sounds.h>';
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_PH20PortInit'] = 'PH20Port buzzerplay('+ dropdown_buzzerinitPort +');\n';
  Blockly.Arduino.definitions_['buzzerplay2'] = 'Buzzer mBuzzer = Buzzer(buzzerplay.pin1());';
  if(dropdown_songs===("S_connection")){
    var code = 'mBuzzer._tone(note_E5, 50, 30);\nmBuzzer._tone(note_E6, 55, 25);\nmBuzzer._tone(note_A6, 60, 10);\n';
  }
  if(dropdown_songs===("S_disconnection")){
    var code = 'mBuzzer._tone(note_E5, 50, 30)\n;mBuzzer._tone(note_A6, 55, 25);\nmBuzzer._tone(note_E6, 50, 10);\n';
  }
  if(dropdown_songs===("S_buttonPushed")){
    var code = 'mBuzzer.bendTones (note_E6, note_G6, 1.03, 20, 2);\ndelay(30);\nmBuzzer.bendTones (note_E6, note_D7, 1.04, 10, 2);\n';
  }
  if(dropdown_songs===("S_mode3")){
    var code = 'mBuzzer._tone(note_E6, 50, 100);\nmBuzzer._tone(note_G6, 50, 80);\nmBuzzer._tone(note_D7, 300, 0);\n ';
  }
  if(dropdown_songs===("S_surprise")){
    var code = 'mBuzzer.bendTones(800, 2150, 1.02, 10, 1);\nmBuzzer.bendTones(2149, 800, 1.03, 7, 1);\n';
  }
  if(dropdown_songs===("S_OhOoh")){
    var code = 'for (int i = 880; i < 2000; i = i * 1.04) {\nmBuzzer._tone(note_B5, 5, 10);\n}\n';
  }
  if(dropdown_songs===("S_OhOoh2")){
    var code = 'for (int i = 1880; i < 3000; i = i * 1.03) {\nmBuzzer._tone(note_C6, 10, 10);\n}\n';
  }
  if(dropdown_songs===("S_cuddly")){
    var code = 'mBuzzer.bendTones(700, 900, 1.03, 16, 4);\nmBuzzer.bendTones(899, 650, 1.01, 18, 7);\n';
  }
  if(dropdown_songs===("S_happy")){
    var code = 'mBuzzer.bendTones(1500, 2500, 1.05, 20, 8);\nmBuzzer.bendTones(2499, 1500, 1.05, 25, 8);\n';
  }
  if(dropdown_songs===("S_sad1")){
    var code = 'mBuzzer.bendTones(1600, 3000, 1.02, 2, 15);\n';
  }
  if(dropdown_songs===("S_sad2")){
    var code = 'mBuzzer.bendTones(1600, 4000, 1.02, 2, 20);\nmBuzzer.bendTones(4000, 3000, 1.02, 2, 20);\n';
  }
  if(dropdown_songs===("S_callPolice")){hong
    Blockly.Arduino.definitions_['include_buzzerplay2'] = '#include <Buzzer.h>\n';
    Blockly.Arduino.definitions_['buzzerplay2'] = 'Buzzer buzzer(buzzerplay.pin1());\n';
    var code = 'for(int i = 200; i <= 800; i++)\n {\n    buzzer.tone(i, 10);\n }\n'+                    //Max Frequency hold 1s   
    ' for(int i= 800; i >= 200; i--)\n {\n    buzzer.tone(i, 10);\n }\n';
  }
  if(dropdown_songs===("S_stop")){
    Blockly.Arduino.definitions_['include_buzzerplay2'] = '#include <Buzzer.h>\n';
    Blockly.Arduino.definitions_['buzzerplay2'] = 'Buzzer buzzer(buzzerplay.pin1());\n';
    var code = 'buzzer.noTone();\n';
  }
  return code;
  }


//mpu
Blockly.Arduino['motorDriverBoard_mpu'] = function(block) {
    var dropdown_mpu2 = this.getFieldValue('mpu2');
    Blockly.Arduino.definitions_['setup_ph_mpu'] = '  mpu6050.InitMpu6050();\n';
    Blockly.Arduino.definitions_['include_ph_mpu'] = '#include "Mpu6050Module.h"\nMpu6050Module mpu6050;\n';
    Blockly.Arduino.definitions_['timer_run_mpu'] = ' mpu6050.GetInclination();\n';
    var code = 'mpu6050.'+dropdown_mpu2;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//红外发送
  Blockly.Arduino['motorDriverBoard_irsend'] = function(block) {
      var dropdown_irsend = this.getFieldValue('irsend');
      var value_irdata2 = Blockly.Arduino.valueToCode(this, 'irdata2', Blockly.Arduino.ORDER_ATOMIC);
      // var value_irbyte2 = Blockly.Arduino.valueToCode(this, 'irbyte2', Blockly.Arduino.ORDER_ATOMIC);
      Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
      Blockly.Arduino.definitions_['include_ph_irsend'] = '#include "IRremote.h"\n';
      Blockly.Arduino.definitions_['include_irsend'] = 'PH20Port irsendPort('+ dropdown_irsend + ");\n";
      Blockly.Arduino.definitions_['ph_irsend'] = 'IRremote irsend(irsendPort.pin1());\n';
      var code = 'irsend.sendNEC(' + value_irdata2.replace(/'/g, '') + ');\n';
      return code;
  }
  //普通遥控器按下
Blockly.Arduino['motorDriverBoard_irKeyPress'] = function(block) {
  var dropdown_irKeyPort = this.getFieldValue('irKeyPort');
  Blockly.Arduino.definitions_['include_IRremote'] = '#include "IRremote.h"\n';
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_keyMap'] = '#include <keymap.h>\n';
  var dropdown_Irkey = this.getFieldValue('irkey');
  Blockly.Arduino.definitions_['include_irKeyPress'] = 'PH20Port irKeyPress('+ dropdown_irKeyPort + ");\n";
  Blockly.Arduino.definitions_['include_IRremoteInit'] = 'IRremote ir(irKeyPress.pin1());\n';
  var code = 'ir.getIrKey(ir.getCode(),1) == '+ dropdown_Irkey;
  return [code, Blockly.Arduino.ORDER_ATOMIC];
  }
  //emakefun遥控器按下
  Blockly.Arduino['motorDriverBoard_emakefunIrKeyPress'] = function(block) {
  //var dropdown_DS18B20ss = this.getFieldValue('DS18B20ss');
    var dropdown_irEmakefunPort = this.getFieldValue('irEmakefunPort');
    Blockly.Arduino.definitions_['include_IRremote'] = '#include "IRremote.h"\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_keyMap'] = '#include <keymap.h>\n';
    var dropdown_irEmakefunkey = this.getFieldValue('irEmakefunkey');
    Blockly.Arduino.definitions_['include_irEmakefunKeyPress'] = 'PH20Port irEmakefunKeyPress('+ dropdown_irEmakefunPort + ");\n";
    Blockly.Arduino.definitions_['include_IRremoteInit'] = 'IRremote irEmakefun(irEmakefunKeyPress.pin1());\n';
    var code = 'irEmakefun.getIrKey(irEmakefun.getCode(),2) == '+ dropdown_irEmakefunkey;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  }
//红外接收
Blockly.Arduino['motorDriverBoard_irreceive'] = function(block) {
    var dropdown_irreceive = this.getFieldValue('irreceive');
    Blockly.Arduino.definitions_['setup_ph_irreceive'] = '  ir.enableIRIn();\n';
    Blockly.Arduino.definitions_['include_ph_IRremote'] = '#include "IRremote.h"\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['ph_IRremote'] = 'PH20Port irPort('+dropdown_irreceive+');\nIRremote ir(irPort.pin1());\n';
    var code = 'ir.getCode() != 0';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//红外接收传感器接收的值
Blockly.Arduino['motorDriverBoard_irreceivedata'] = function(block) {
      Blockly.Arduino.definitions_['setup_ph_irreceive'] = '  ir.enableIRIn();\n';
    //var dropdown_track = this.getFieldValue('track');
    //  Blockly.Arduino.definitions_['setup_ph_track'] = 'pinMode('+dropdown_track+', INTPUT);';
    	//Blockly.Arduino.definitions_['ph_led'] = '';
      var code = 'ir.getCode()';
      	return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//红外接收传感器接收下一个值
Blockly.Arduino['motorDriverBoard_irreceivenext'] = function(block) {
  return  'ir.resume();\n';
}
//有源蜂鸣器
Blockly.Arduino['motorDriverBoard_buzzer'] = function(block) {
    var dropdown_buzzer = this.getFieldValue('buzzer');
    var value_buzzers = Blockly.Arduino.valueToCode(this, 'buzzers', Blockly.Arduino.ORDER_ATOMIC);
    var value_time = Blockly.Arduino.valueToCode(this, 'time', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_buzzer'] = '#include "Buzzer.h"\nPH20Port passivebuzzerPort('+ dropdown_buzzer + ");\n"+'Buzzer buzzer('+ dropdown_buzzer + ");\n";
  return  'buzzer.tone(passivebuzzerPort.pin1(),'+value_buzzers+','+value_time+');\n';
}
//ph_passive_buzzer
Blockly.Arduino['motorDriverBoard_buzzer1'] = function(block) {
  var dropdown_buzzer1 = this.getFieldValue('passiveBuzzer');
  // var value_buzzers = Blockly.Arduino.valueToCode(this, 'buzzers', Blockly.Arduino.ORDER_ATOMIC);
  var value_time1 = Blockly.Arduino.valueToCode(this, 'play_time', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_passive_buzzer'] = 'PH20Port buzzer1('+ dropdown_buzzer1 + ");\n";
  return  'for(int i=0;i<'+value_time1 +';i++)\n{\nbuzzer1.dWrite1(HIGH);\n}\n';
}
//直流电机
Blockly.Arduino['motorDriverBoard_motor'] = function(block) {
  var dropdown_motor = this.getFieldValue('motor');
  var dropdown_direction = this.getFieldValue('direction');
  var value_motorSpeed = Blockly.Arduino.valueToCode(this, 'motorSpeed', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['includemotor'] = 'PH20Port motor('+ dropdown_motor + ");\n";
  if(dropdown_direction === 'forward'){
    return  'motor.aWrite1('+ value_motorSpeed +');\nmotor.aWrite2(0);\n';
  }else{
    return  'motor.aWrite2('+ value_motorSpeed +');\nmotor.aWrite1(0);\n';
  }
}

//双电机驱动
Blockly.Arduino['motorDriverBoard_dcMotorDriver'] = function(block) {
  var dropdown_driverPin = this.getFieldValue('driverPin');
  var dropdown_motor1Direction = this.getFieldValue('motor1');
  var dropdown_motor2Direction = this.getFieldValue('PINS2');
  var dropdown_pwm1 = this.getFieldValue('pwm1');
  var dropdown_pwm2 = this.getFieldValue('pwm2');
  Blockly.Arduino.definitions_['dcmotor2'] = ' void motorModule2(int IN1, int IN2, int pinPwm, int speed){\n    pinMode(IN1, OUTPUT);\n    pinMode(IN2, OUTPUT);\n    if(speed>=0){\n        digitalWrite(IN1, 1);\n        digitalWrite(IN2, 0);\n    }else{\n        digitalWrite(IN1, 0);\n        digitalWrite(IN2, 1);\n    }\n    analogWrite(pinPwm, abs(speed));\n}\n';
  return  'motorModule2('+dropdown_PINS1+', '+dropdown_PINS2+','+dropdown_pwm+', '+value_dcmotors2+');\n';
  }


//继电器
Blockly.Arduino['motorDriverBoard_relay'] = function(block) {
    var dropdown_relay = this.getFieldValue('relay');
    var dropdown_status2 = this.getFieldValue('status2');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_relay'] = 'PH20Port relay('+ dropdown_relay + ");\n";
    var code = 'relay.dWrite1('+dropdown_status2+');\n';
    return code;
}
//  tm1650数码管
Blockly.Arduino['motorDriverBoard_tm1650'] = function(block) {
    // var dropdown_tm1650 = this.getFieldValue('tm1650');
    var dropdown_status3 = this.getFieldValue('status3');
  //var value_irdata2 = Blockly.Arduino.valueToCode(this, 'irdata2', Blockly.Arduino.ORDER_ATOMIC);
  //var value_irbyte2 = Blockly.Arduino.valueToCode(this, 'irbyte2', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_ph_tm1650'] = '#include <TM1650.h>\n#include <Wire.h>\n';
    Blockly.Arduino.definitions_['setup_ph_tm1650'] = 'Wire.begin();\n   tm_4display.init();\n';
  Blockly.Arduino.definitions_['ph_tm1650'] = 'TM1650 tm_4display;\n';
    var code = ' tm_4display.'+dropdown_status3+';\n';
      return code;
}
//tm1650数码管显示
Blockly.Arduino['motorDriverBoard_tm1650display'] = function(block) {
  var value_tm1650displays = Blockly.Arduino.valueToCode(this, 'tm1650displays', Blockly.Arduino.ORDER_ATOMIC);
  //var value_irdata2 = Blockly.Arduino.valueToCode(this, 'irdata2', Blockly.Arduino.ORDER_ATOMIC);
  //var value_irbyte2 = Blockly.Arduino.valueToCode(this, 'irbyte2', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_ph_tm1650'] = '#include <TM1650.h>\n#include <Wire.h>\n';
    Blockly.Arduino.definitions_['setup_ph_tm1650'] = 'Wire.begin();\n  tm_4display.init();\n';
    Blockly.Arduino.definitions_['ph_tm1650'] = 'TM1650 tm_4display;\n';
    var code = 'tm_4display.displayString('+value_tm1650displays.replace(/'/g, '"')+');\n';
      return code;
}
//tm1650数码管显示小数点
Blockly.Arduino['motorDriverBoard_tm1650displaypoint'] = function(block) {
  var dropdown_tm1650displaypoint = this.getFieldValue('tm1650displaypoint');
  var dropdown_tm1650displaypointnum = this.getFieldValue('tm1650displaypointnum');
    Blockly.Arduino.definitions_['include_ph_tm1650'] = '#include <TM1650.h>\n#include <Wire.h>\n';
    Blockly.Arduino.definitions_['setup_ph_tm1650'] = 'Wire.begin();\n  tm_4display.init();\n';
    Blockly.Arduino.definitions_['ph_tm1650'] = 'TM1650 tm_4display;\n';
    var code = 'tm_4display.setDot('+dropdown_tm1650displaypoint+','+dropdown_tm1650displaypointnum+');\n';
      return code;
}
//tm1637初始化
Blockly.Arduino['motorDriverBoard_tm1637'] = function(block) {
  var dropdown_tm1637 = this.getFieldValue('tm1637');
  //var dropdown_tm1650displaypointnum = this.getFieldValue('tm1650displaypointnum');
    Blockly.Arduino.definitions_['include_ph_timerOne'] = '#include "TimerOne.h"\n';
    Blockly.Arduino.definitions_['include_ph_tm1637'] = '#include "TM1637.h"\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ON'] = '#define ON 1\n';
    Blockly.Arduino.definitions_['include_OFF'] = '#define OFF 0\n';
    Blockly.Arduino.definitions_['setup_ph_tm1637'] = 'tm1637.begin();\n';
 
    Blockly.Arduino.definitions_['setup_ph_initialize'] = 'Timer1.initialize(500000);\n';
    Blockly.Arduino.definitions_['setup_ph_attachInterrupt'] = 'Timer1.attachInterrupt(TimingISR);\n';
    Blockly.Arduino.definitions_['ph_tm1637'] = 'PH20Port tm1637Port('+dropdown_tm1637 +');\n';
    Blockly.Arduino.definitions_['ph_TimeDisp'] = 'String TimeDisp = "";\n';
    Blockly.Arduino.definitions_['ph_ClockPoint'] = 'unsigned char ClockPoint = 1;\n';
    Blockly.Arduino.definitions_['ph_Update'] = 'unsigned char Update;\n';
    Blockly.Arduino.definitions_['ph_halfsecond'] = 'unsigned char halfsecond = 0;\n';
    Blockly.Arduino.definitions_['ph_second'] = 'unsigned char second;\n';
    Blockly.Arduino.definitions_['ph_minute'] = 'unsigned char minute;\n';
    Blockly.Arduino.definitions_['ph_hour'] = 'unsigned char hour;\n';
    Blockly.Arduino.definitions_['ph_display'] = 'TM1637  tm1637(tm1637Port.pin1(),tm1637Port.pin2());\n';
    Blockly.Arduino.definitions_['ph_timingISR'] = 'void TimingISR()\n'+
    '{\n'+
    '  halfsecond ++;\n'+
    '  Update = ON;\n'+
    '  if(halfsecond == 2){\n'+
    '    second ++;\n'+
    '    if(second == 60)\n'+
    '    {\n'+
    '      minute ++;\n'+
    '      if(minute == 60)\n'+
    '      {\n'+
    '        hour ++;\n'+
    '        if(hour == 24)\n'+
    '        hour = 0;\n'+
    '        minute = 0;\n'+
    '      }\n'+
    '      second = 0;\n'+
    '    }\n'+
    '    halfsecond = 0;\n'+  
    '  }\n'+
    '  ClockPoint = (~ClockPoint) & 0x01;\n'+
    '}\n';
    Blockly.Arduino.definitions_['ph_timeUpdate'] ='void TimeUpdate(void)\n'+
    '{\n'+
    '  if(ClockPoint)tm1637.setColonOn(false);\n'+
    '  else tm1637.setColonOn(true);\n'+
    '  TimeDisp = hour / 10;\n'+
    '  TimeDisp += hour % 10;\n'+
    '  TimeDisp += minute / 10;\n'+
    '  TimeDisp += minute % 10;\n'+
    '  Update = OFF;\n'+
    '}\n';
    var code = '';
      return code;
}
//tm1637设置亮度
Blockly.Arduino['motorDriverBoard_tm1637setlight'] = function(block) {
    var value_tm1637setlights = Blockly.Arduino.valueToCode(this, 'tm1637setlights', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'tm1637.setBacklight('+value_tm1637setlights+');\n';
      return code;
}
//tm1637显示
Blockly.Arduino['motorDriverBoard_tm1637display'] = function(block) {
    var value_tm1637displays = Blockly.Arduino.valueToCode(this, 'tm1637displays', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'tm1637.print('+value_tm1637displays.replace(/'/g, '"')+');\n';
      return code;
}

//tm1637显示
Blockly.Arduino['motorDriverBoard_tm1637displayPoint'] = function(block) {
  var dropdown_tm1637displaypointnum = this.getFieldValue('tm1637displaypointnum');
  var code = 'tm1637.setColonOn('+dropdown_tm1637displaypointnum+');\n';
    return code;
}
//tm1637显示时间
Blockly.Arduino['motorDriverBoard_tm1637displaytime'] = function(block) {
    var value_tm1637displaytime1 = Blockly.Arduino.valueToCode(this, 'tm1637displaytime1', Blockly.Arduino.ORDER_ATOMIC);
      var value_tm1637displaytime2 = Blockly.Arduino.valueToCode(this, 'tm1637displaytime2', Blockly.Arduino.ORDER_ATOMIC);
  //var dropdown_tm1650displaypointnum = this.getFieldValue('tm1650displaypointnum');
    // Blockly.Arduino.definitions_['include_ph_tm1637'] = '#include <SevenSegmentExtended.h>\n';
    Blockly.Arduino.definitions_['setup_minute'] = 'minute = '+value_tm1637displaytime2+';\n';
    Blockly.Arduino.definitions_['setup_hour'] = 'hour = '+value_tm1637displaytime1+';\n';
    // Blockly.Arduino.definitions_['ph_display'] = 'SevenSegmentExtended  display(tm1637.pin1(),tm1637.pin2());\n';
    
    var code ='  if(Update == ON)\n'+
    '  {\n'+
    '    TimeUpdate();\n'+
    '    tm1637.print(TimeDisp);\n'+
    '  }\n';
      return code;
}
//tm1637清屏
Blockly.Arduino['motorDriverBoard_tm1637clear'] = function(block) {
  //  var value_tm1637setlights = Blockly.Arduino.valueToCode(this, 'tm1637setlights', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'display.clear();\n';
      return code;
}
//rgb
Blockly.Arduino['motorDriverBoard_rgb'] = function(block) {
    var value_rgb1 = Blockly.Arduino.valueToCode(this, 'rgb1', Blockly.Arduino.ORDER_ATOMIC);
      var value_rgb2 = Blockly.Arduino.valueToCode(this, 'rgb2', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_rgb = this.getFieldValue('rgb');
  //Blockly.Arduino.definitions_['setup_ph_rgb'] = '';
    Blockly.Arduino.definitions_['include_ph_rgb'] = '#include <Adafruit_NeoPixel.h>\n';
    Blockly.Arduino.definitions_['ph_rgb'] = 'Adafruit_NeoPixel  rgb_display= Adafruit_NeoPixel('+value_rgb1+','+dropdown_rgb+',NEO_GRB + NEO_KHZ800);\n';
    var code = 'rgb_display.begin();\n rgb_display.setBrightness('+value_rgb2+');\n';
      return code;
}
//rgb颜色
Blockly.Arduino['motorDriverBoard_rgbcolor'] = function(block) {
  var value_rgbPort = this.getFieldValue('rgb');
  var dropdown_rgbColor = block.childBlocks_[0].colour_; 
  var colorStr = dropdown_rgbColor.colorRgb();
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_port'] = 'PH20Port rgbPort('+ value_rgbPort +');\n';
  var red_color = 0;
  var blue_color = 0;
  var green_color = 0;
  //Blockly.Arduino.definitions_['setup_ph_rgb'] = '';
  //  Blockly.Arduino.definitions_['include_ph_rgb'] = '#include <Adafruit_NeoPixel.h>\n';
  //  Blockly.Arduino.definitions_['ph_rgb'] = 'Adafruit_NeoPixel  rgb_display= Adafruit_NeoPixel('+value_rgb1+','+dropdown_rgb+',NEO_GRB + NEO_KHZ800);\n';
  if (colorStr) {
      var colorArr = colorStr.split(",");
      red_color = colorArr[0];
      green_color = colorArr[1];
      blue_color = colorArr[2];
   }

  var code = 'rgbPort.aWrite3('+ blue_color +');\n rgbPort.aWrite4('+ red_color + ');\n rgbPort.aWrite5('+ green_color +');\n';
  return code;
}

String.prototype.colorRgb = function(){
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
	var sColor = this.toLowerCase();
	if(sColor && reg.test(sColor)){
		if(sColor.length === 4){
			var sColorNew = "#";
			for(var i=1; i<4; i+=1){
				sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));	
			}
			sColor = sColorNew;
		}
		//处理六位的颜色值
		var sColorChange = [];
		for(var i=1; i<7; i+=2){
			sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));	
		}
		return  sColorChange.join(",") ;
	}else{
		return sColor;	
	}
};

//rgb环初始化
Blockly.Arduino['motorDriverBoard_ringRgbColorInit'] = function(block) {
  var value_rgbPort = this.getFieldValue('rgbRing');
  
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_ring'] = '#include "RGBLed.h"\nPH20Port rgbRing('+ value_rgbPort +');\n';
  // Blockly.Arduino.definitions_['include_mrgb'] = '#include "RGBLed.h"\nPH20Port rgbRing('+ value_rgbPort +');\n';
  // Blockly.Arduino.definitions_['setup_begin'] = 'pixels.begin();';
  // Blockly.Arduino.definitions_['include_ph_pixels'] = 'Adafruit_NeoPixel pixels = Adafruit_NeoPixel(12, A0, NEO_GRB + NEO_KHZ800);';
  return '';
}
// 设置RGB环亮度
Blockly.Arduino['motorDriverBoard_setRingRgbLight'] = function(block) {
  var value_rgbLight = Blockly.Arduino.valueToCode(this, 'rgbLight', Blockly.Arduino.ORDER_ATOMIC);
   var code = 'mRgb.setBrightness('+ value_rgbLight +');\n';
  return code;
}
//rgb环
Blockly.Arduino['motorDriverBoard_ringRgbColor'] = function(block) {
  var value_total = Blockly.Arduino.valueToCode(this, 'total', Blockly.Arduino.ORDER_ATOMIC);
  var value_start = Blockly.Arduino.valueToCode(this, 'start', Blockly.Arduino.ORDER_ATOMIC);
  var value_end = Blockly.Arduino.valueToCode(this, 'end', Blockly.Arduino.ORDER_ATOMIC);
  var value_red = 0;
  var value_green = 0;
  var value_blue = 0;
  var value_lightTime = Blockly.Arduino.valueToCode(this, 'lightTime', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_mrgb'] = 'RGBLed mRgb(rgbRing.pin1(),'+ value_total+');\n';
  Blockly.Arduino.definitions_['include_ph_time'] = 'int delayval = '+ value_lightTime +';';
  var dropdown_rgbColor = block.childBlocks_[4].colour_; 
  var colorStr = dropdown_rgbColor.colorRgb();
  if (colorStr) {
    var colorArr = colorStr.split(",");
    value_red = colorArr[0];
    value_green = colorArr[1];
    value_blue = colorArr[2];
 }
  // Blockly.Arduino.definitions_['include_ph_pixels'] = 'Adafruit_NeoPixel pixels = Adafruit_NeoPixel('+value_total+', rgbRing.pin1(), NEO_GRB + NEO_KHZ800);';
  var code = 'for(int i='+value_start +'-1;i<'+ value_end +';i++){\n    mRgb.setColorAt(i, ' + value_red + ', ' + value_green + ', ' + value_blue + '); \n    mRgb.show(); \n'+
    '    delay(delayval);\n}';
  return code;
}
// 摇杆
Blockly.Arduino['motorDriverBoard_rock'] = function(block) {
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  
    var dropdown_rock = this.getFieldValue('rock');
    var dropdown_rock2 = this.getFieldValue('rock2');
    var code = '';
    Blockly.Arduino.definitions_['include_ph_rock'] = 'PH20Port rock('+ dropdown_rock +');\n';
    // dropdown_rock2 = dropdown_rock2 == "D2"?2:dropdown_rock2;
    // if(dropdown_rock2 === "D2"){
    //   code = 'digitalRead(2)';
    // }else{
    //   code = 'analogRead('+dropdown_rock2+')';
    // }
    // Blockly.Arduino.definitions_['setup_ph_rock'] = '  pinMode('+dropdown_rock2+', INPUT);\n';
      //Blockly.Arduino.definitions_['ph_led'] = '';  
      
    return [dropdown_rock2, Blockly.Arduino.ORDER_ATOMIC];
}
//钢琴模块
Blockly.Arduino['motorDriverBoard_piano'] = function(block) {
    var dropdown_piano = this.getFieldValue('piano');
    var dropdown_piano2 = this.getFieldValue('piano2');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_piano'] = '#include <BS818A.h>\nPH20Port piano('+ dropdown_piano + ");\n";
    Blockly.Arduino.definitions_['setup_ph_piano'] = '  BS.InitBS818A(piano.pin2(), piano.pin1());\n';
    Blockly.Arduino.definitions_['ph_piano'] = 'BS818A BS;\n';
    var code = 'BS.PressBsButton('+dropdown_piano2+')';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//旋转电位器
Blockly.Arduino['ph_resistance'] = function(block) {
    var dropdown_resistance = this.getFieldValue('resistance');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_piano'] = 'PH20Port resistance('+ dropdown_resistance + ");\nfloat voltage=0,data;";
    Blockly.Arduino.definitions_['timer_run_resistance'] = 'data=resistance.aRead1();\nvoltage=(data/1023)*5;\n';
      // Blockly.Arduino.definitions_['setup_ph_resistance'] = 'pinMode('+dropdown_resistance+', INPUT);\n';
    var code = 'voltage';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//滑动电位器
Blockly.Arduino['motorDriverBoard_slipresistance'] = function(block) {
    var dropdown_slipresistance = this.getFieldValue('slipresistance');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_piano'] = 'PH20Port slidePotentiometer('+ dropdown_slipresistance + ");\nfloat i=0,data;\nint j = 0;\n";
    // Blockly.Arduino.definitions_['setup_ph_slipresistance'] = 'pinMode('+dropdown_slipresistance+', INPUT);\n';
    Blockly.Arduino.definitions_['timer_run_slip'] = ' data=slidePotentiometer.aRead1();\n  i=data/1023;\n  j=(1-i)*100;\n';
    var code = 'j';
    return [code, Blockly.Arduino.ORDER_ATOMIC];j
}
//红绿灯
Blockly.Arduino['motorDriverBoard_trafficlights'] = function(block) {
    var trafficlights  = this.getFieldValue('trafficlights');
    // var dropdown_trafficlights1 = this.getFieldValue('trafficlights1');
    var dropdown_trafficlightsR = this.getFieldValue('trafficlights2');
    var dropdown_trafficlightsG = this.getFieldValue('trafficlights3');
    var dropdown_trafficlightsY = this.getFieldValue('trafficlights4');
    var lightTime = Blockly.Arduino.valueToCode(this, 'lightTime', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_trafficlights'] = 'PH20Port trafficlights('+ trafficlights + ");\n";

    var code = 'trafficlights.dWrite3('+dropdown_trafficlightsG +');\n trafficlights.dWrite4('+dropdown_trafficlightsY +');\n trafficlights.dWrite5('+dropdown_trafficlightsR +');\n delay('+ lightTime +');\n';
      return code;
}
//流水灯
Blockly.Arduino['motorDriverBoard_waterlights'] = function(block) {
    var dropdown_waterlights = this.getFieldValue('waterlights');
    var dropdown_waterlights1 = this.getFieldValue('waterlights1');
  Blockly.Arduino.definitions_['setup_ph_waterlights'] = 'pinMode('+dropdown_waterlights+', OUTPUT);\n';
    var code = 'digitalWrite('+dropdown_waterlights+','+dropdown_waterlights1+');\n';
      return code;
}
//8x8点阵屏初始化
Blockly.Arduino['motorDriverBoard_max'] = function(block) {
    var dropdown_max = this.getFieldValue('max');
    var value_maxs = Blockly.Arduino.valueToCode(this, 'maxs', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['setup_ph_max'] = '  ledmatrix.init();\n  ledmatrix.setIntensity(1);\n  ledmatrix.clearMatrix();\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_max'] = '#include <MaxMatrix.h>\nPH20Port matrix('+ dropdown_max +');';
    Blockly.Arduino.definitions_['ph_max'] = 'MaxMatrix ledmatrix(matrix.pin5(),matrix.pin4(),matrix.pin3(),'+value_maxs+');\n';
    return '';
}
//8x8点阵屏单点亮或灭
Blockly.Arduino['motorDriverBoard_maxpoint'] = function(block) {
    var dropdown_draw = this.getFieldValue('draw');
    // var value_maxpoint1 = Blockly.Arduino.valueToCode(this, 'maxpoint1', Blockly.Arduino.ORDER_ATOMIC);
    // var value_maxpoint2 = Blockly.Arduino.valueToCode(this, 'maxpoint2', Blockly.Arduino.ORDER_ATOMIC);
    return 'ledmatrix.drawShape(ledmatrix.getMouthShape('+dropdown_draw+'));';
}

//8x8点阵屏单点亮
Blockly.Arduino['motorDriverBoard_x8leddisplay'] = function(block) {
  var dropdown_draw = this.getFieldValue('draws');
  // var value_maxpoint1 = Blockly.Arduino.valueToCode(this, 'maxpoint1', Blockly.Arduino.ORDER_ATOMIC);
  // var value_maxpoint2 = Blockly.Arduino.valueToCode(this, 'maxpoint2', Blockly.Arduino.ORDER_ATOMIC);
  return 'ledmatrix.writeFull('+dropdown_draw+');';
}

	//手势传感器
  Blockly.Arduino['motorDriverBoard_handle_gesture'] = function(block) {
    // Blockly.Arduino.definitions_['breaks_value'] = 'int value = apds.readGesture();\n';
    Blockly.Arduino.definitions_['define_ph_handle_gesture_status'] = 'int gestureStatus;\n';
    
    var dropdown_status = this.getFieldValue('gestureStatus');
    var code = 'gestureStatus == '+ dropdown_status;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  }

  //手势传感器初始化
  Blockly.Arduino['motorDriverBoard_handleGestureInit'] = function(block) {
    Blockly.Arduino.definitions_['include_ph_handle_gesture'] = '#include <Wire.h>\n#include "SparkFun_APDS9960.h"\n';
    Blockly.Arduino.definitions_['define_ph_handle_gesture'] = 'SparkFun_APDS9960 apds = SparkFun_APDS9960();\n';
    Blockly.Arduino.definitions_['setup_ph_handle_gesture_sensor'] = '  apds.init();\n  apds.enableGestureSensor(true);\n';
    // var code = 'apds.enableGestureSensor(true)';
    return "";
  }

  //手势传感器是否获取到手势
  Blockly.Arduino['motorDriverBoard_gesture'] = function(block) {
    var code = 'apds.isGestureAvailable()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  }

  //手势传感器状态
  Blockly.Arduino['motorDriverBoard_gestureStatus'] = function(block) {
    // var dropdown_status = this.getFieldValue('gestureStatus');
    // Blockly.Arduino.definitions_['timer_run_handle_status'] = 'gestureStatus = apds.readGesture();\n';
    var code = 'gestureStatus = apds.readGesture();\n' ;
    return code;
  }

  
   //旋转编码器初始化
   Blockly.Arduino['motorDriverBoard_ec11Init'] = function(block) {
    var dropdown_rotaryPort = this.getFieldValue('rotary');
    // var dropdown_rotaryPort = this.getFieldValue('rotary');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_motorDriverBoard_ec11Init'] = 'PH20Port rotaryEncoder('+ dropdown_rotaryPort +');\n';
    Blockly.Arduino.definitions_['define_motorDriverBoard_ec11Val'] = 'long position, oldposition;\n';
    Blockly.Arduino.definitions_['setup_motorDriverBoard_ec11Port'] = '  attachInterrupt(1, read_quadrature, CHANGE);\n';
    Blockly.Arduino.definitions_['define_motorDriverBoard_read_quadrature'] = 'void read_quadrature(){\n' +   
    '  if (rotaryEncoder.dRead3() == LOW){\n'+   
    '    if (rotaryEncoder.dRead4() == LOW)\n'+
    '      position++;\n'+
    '  }\n'+
    '  else {\n'+
    '    if (rotaryEncoder.dRead4() == LOW)\n'+
    '      position--;\n'+
    '  }\n'+
    '  oldposition = position;\n'+
    '}\n';
    return '';
  }


  //旋转编码器按钮是否被按下
  Blockly.Arduino['motorDriverBoard_ec11ButtonPushDown'] = function(block) {
    // Blockly.Arduino.definitions_['breaks_value'] = 'int value = apds.readGesture();\n';
    var code = 'rotaryEncoder.dRead5() == LOW';
    return [code, Blockly.Arduino.ORDER_ATOMIC];;
  }

    //磁簧开关传感器是否检测到磁场
    Blockly.Arduino['motorDriverBoard_reed_switch'] = function(block) {
      var dropdown_reedPort = this.getFieldValue('reedPort');
      Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
      Blockly.Arduino.definitions_['include_ph_reed_switch'] = 'PH20Port reedSwitch('+ dropdown_reedPort +');\n';
      // Blockly.Arduino.definitions_['breaks_value'] = 'int value = apds.readGesture();\n';
      var code = 'reedSwitch.dRead1()==LOW';
      return [code, Blockly.Arduino.ORDER_ATOMIC];;
    }

      //磁簧开关传感器是否检测到磁场
      Blockly.Arduino['motorDriverBoard_tilt'] = function(block) {
        var dropdown_tilt = this.getFieldValue('tilt');
        Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
        Blockly.Arduino.definitions_['include_tilt'] = 'PH20Port tilt('+ dropdown_tilt +');\n';
        // Blockly.Arduino.definitions_['breaks_value'] = 'int value = apds.readGesture();\n';
        var code = 'tilt.dRead1()==LOW';
        return [code, Blockly.Arduino.ORDER_ATOMIC];;
      }

  //旋转编码器
  Blockly.Arduino['motorDriverBoard_ec11'] = function(block) {
    // Blockly.Arduino.definitions_['timer_run_ec11'] = ' count_A = rotaryEncoder.dRead3();\n  if(count_A!=lastcount_A)\n  {\n    if(rotaryEncoder.dRead4()!= count_A)\n'+
    // '    {\n      counter++;\n    }\n    else\n    {\n     counter--;\n    }\n  }\n  lastcount_A = count_A;\n';
    // var code = "counter";
    return ['position', Blockly.Arduino.ORDER_ATOMIC];;
  }
//灰度传感器器
Blockly.Arduino['motorDriverBoard_grayscale'] = function(block) {
  var dropdown_grayscale = this.getFieldValue('grayscale');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_grayscale'] = 'PH20Port grayscale('+ dropdown_grayscale +');\n';
  var code ='grayscale.aRead1()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];;
}

//震动传感器器
Blockly.Arduino['motorDriverBoard_shock'] = function(block) {
  var dropdown_shock = this.getFieldValue('shockPin');
  var dropdown_shockValue = this.getFieldValue('shockValue');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_shock' + dropdown_shock] = 'PH20Port shock('+ dropdown_shock +');\n';
  var code ='shock.'+dropdown_shockValue;
  return [code, Blockly.Arduino.ORDER_ATOMIC];;
}

//超声波读值
Blockly.Arduino['motorDriverBoard_ultrasonic'] = function(block) {
  var dropdown_shock = this.getFieldValue('ultrasonic');
  // var dropdown_shockValue = this.getFieldValue('shockValue');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_ultrasonic' + dropdown_shock] = 'PH20Port ultrasonic('+ dropdown_shock +');\nfloat distance;\n';
  Blockly.Arduino.definitions_['timer_run_ultrasonic'] = 'ultrasonic.dWrite1(LOW);\ndelayMicroseconds(2);\nultrasonic.dWrite1(HIGH);\ndelayMicroseconds(10);\n'+
  'ultrasonic.dWrite1(LOW);\ndistance = pulseIn(ultrasonic.pin2(), HIGH) / 58.00;\n';
  var code ='distance';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//舵机
Blockly.Arduino['motorDriverBoard_servo'] = function(block) {
  var dropdown_servopins = this.getFieldValue('phservopins');
  // Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_servo'] = '#include <Servo.h>\n';
  Blockly.Arduino.definitions_['include_ph_servo' + dropdown_servopins] = 'Servo servo_'+ dropdown_servopins + ';\n';
  Blockly.Arduino.definitions_['setup_servoInit' + dropdown_servopins] = '  servo_'+ dropdown_servopins + '.attach('+dropdown_servopins +');\n';
  var value_servoangle = Blockly.Arduino.valueToCode(this, 'phservoangle', Blockly.Arduino.ORDER_ATOMIC);//
    return 'servo_'+dropdown_servopins+'.write('+value_servoangle+');\n';
  }

  //串口波特率
Blockly.Arduino['motorDriverBoard_serialbegin'] = function(block) {
	var dropdown_SerialS1 = this.getFieldValue('SerialS1');
var dropdown_serialbegins = this.getFieldValue('serialbegins');
//Blockly.Arduino.definitions_['setup_serialbegin'] = '';
return  ''+dropdown_SerialS1+'.begin(' + dropdown_serialbegins + '); \n';
}
//串口打印字符不换行
Blockly.Arduino['motorDriverBoard_serialprintstring'] = function(block) {
	var dropdown_SerialS22 = this.getFieldValue('SerialS22');
var value_serialprints1 = Blockly.Arduino.valueToCode(this, 'serialprints1', Blockly.Arduino.ORDER_ATOMIC);
return  ''+dropdown_SerialS22+'.print(' + value_serialprints1.replace(/'/g, '"') + '); \n';
}
//串口打印字符
Blockly.Arduino['motorDriverBoard_serialprint'] = function(block) {
	var dropdown_SerialS2 = this.getFieldValue('SerialS2');
var value_serialprints = Blockly.Arduino.valueToCode(this, 'serialprints', Blockly.Arduino.ORDER_ATOMIC);
return  ''+dropdown_SerialS2+'.println(' + value_serialprints.replace(/'/g, '"') + '); \n';
}
//串口打印数字不换行
Blockly.Arduino['motorDriverBoard_serialprintnum1'] = function(block) {
		var dropdown_SerialS33 = this.getFieldValue('SerialS33');
var value_serialprintsss = Blockly.Arduino.valueToCode(this, 'serialprintsss', Blockly.Arduino.ORDER_ATOMIC);
return  ''+dropdown_SerialS33+'.print(' + value_serialprintsss + '); \n';
}
//串口打印数字
Blockly.Arduino['motorDriverBoard_serialprintnum'] = function(block) {
		var dropdown_SerialS3 = this.getFieldValue('SerialS3');
var value_serialprintss = Blockly.Arduino.valueToCode(this, 'serialprintss', Blockly.Arduino.ORDER_ATOMIC);
return  ''+dropdown_SerialS3+'.println(' + value_serialprintss + '); \n';
}
//串口打印16进制
Blockly.Arduino['motorDriverBoard_serialprint16num'] = function(block) {
	var dropdown_SerialS4 = this.getFieldValue('SerialS4');
var value_serialprint16nums = Blockly.Arduino.valueToCode(this, 'serialprint16nums', Blockly.Arduino.ORDER_ATOMIC);
return  ''+dropdown_SerialS4+'.println('+value_serialprint16nums+',HEX);\n';
}
//串口有数据可读
Blockly.Arduino['motorDriverBoard_serialava'] = function(block) {
	var dropdown_SerialS = this.getFieldValue('SerialS');
var code = ''+dropdown_SerialS+'.available() > 0';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//串口读取字节
Blockly.Arduino['motorDriverBoard_serialread'] = function(block) {
		var dropdown_Serials = this.getFieldValue('Serials');
 var code = ''+dropdown_Serials+'.read()';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//软串口
Blockly.Arduino['motorDriverBoard_softserial'] = function(block) {
var dropdown_TX1 = this.getFieldValue('TX1');
var dropdown_RX1 = this.getFieldValue('RX1');
Blockly.Arduino.definitions_['include_softserial'] = '#include <SoftwareSerial.h>';
Blockly.Arduino.definitions_['softserial'] = 'SoftwareSerial softwareSerial = SoftwareSerial('+dropdown_RX1+', '+dropdown_TX1+');';
var code = '';
 return code;
}
//串口中断
Blockly.Arduino['motorDriverBoard_serial_event'] = function(block) {

  var serial_select = this.getFieldValue('Serials');
    var funcName = 'attachPinInterrupt_fun_' + serial_select;
    var branch = Blockly.Arduino.statementToCode(this, 'DO');
    var code2 = ' void '+serial_select+'Event() {\n' + branch + '}\n';
    Blockly.Arduino.definitions_['func_'+funcName+''] = code2;
    return "";

}
//串口清空缓存数据
Blockly.Arduino['motorDriverBoard_serial_flush'] = function(block) {

  var serial_select = this.getFieldValue('Serials');
    return ''+serial_select+'.flush();\n';

}
//串口读取字符串直到
Blockly.Arduino['motorDriverBoard_serial_read'] = function(block) {
	var serial_select = this.getFieldValue('Serials');
var value_serialprints2 = Blockly.Arduino.valueToCode(this, 'serialprints2', Blockly.Arduino.ORDER_ATOMIC);
 var code = ''+serial_select+'.readStringUntil('+value_serialprints2+')';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//串口读取整数小数
Blockly.Arduino['motorDriverBoard_serialreadint'] = function(block) {
		var dropdown_Serials9 = this.getFieldValue('Serials9');
    var dropdown_intfloat = this.getFieldValue('intfloat');
 var code = ''+dropdown_Serials9+'.'+dropdown_intfloat+'';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//初始化矩阵键盘
Blockly.Arduino['motorDriverBoard_matrixKeyboardInit'] = function(block) {
var dropdown_port = this.getFieldValue('matrixKeyBoardPort');
Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
Blockly.Arduino.definitions_['include_TTP229'] = '#include "TTP229.h"\n';
Blockly.Arduino.definitions_['include_TTP229Init'] = 'PH20Port matrixKeyPort('+dropdown_port +');\nTTP229 mTTP229;\n';
Blockly.Arduino.definitions_['setup_TTP229'] = '  mTTP229.initTTP229(matrixKeyPort.pin1(), matrixKeyPort.pin2());\n';
return '';
}


//矩阵键盘 按下按钮 状态判断
Blockly.Arduino['motorDriverBoard_matrixKeyboardFunc'] = function(block) {
    var dropdown_keyBoardValue = this.getFieldValue('ph_keyBoardValue');
    var dropdown_keyBoardStatus = this.getFieldValue('ph_keyBoardCorrespondValue');
    Blockly.Arduino.definitions_['timer_run_matrixkeyBoard'] = ' String key_name = mTTP229.GetKeyMap();\n  char * result = (char *)key_name.c_str();\n ';
    var code ='';
    if(dropdown_keyBoardStatus === 'press'){
      code = 'result == "'+ dropdown_keyBoardValue +'"' ;
    }else if(dropdown_keyBoardStatus === 'release'){
      code = 'result != "'+ dropdown_keyBoardValue +'"' ;
    }else{
      Blockly.Arduino.definitions_['initMatrixKeyBoardFlag'] = 'boolean matrixKeyBoardFlag;\n'
        Blockly.Arduino.definitions_['timer_run_matrixKeyBoard3'] = '  for( int i = 0 ;i < 3000;i++ ){\n'+
          '   if(result == "'+ dropdown_keyBoardValue +'"){\n'+
            '     delay(1);\n'+
            '      matrixKeyBoardFlag = true;\n'+
          '   }else{\n'+
            '     matrixKeyBoardFlag = false;\n'+
            '     delay(1);\n'+
            '     break;\n'+
            '   \n'+
            '   }\n'
        code = 'matrixKeyBoardFlag';
    }
    return  [code, Blockly.Arduino.ORDER_ATOMIC];;
  }
  
  //红外接收传感器接收的值
Blockly.Arduino['motorDriverBoard_getMatrixKeyboardValue'] = function(block) {
  Blockly.Arduino.definitions_['timer_run_matrixkeyBoard'] = ' String key_name = mTTP229.GetKeyMap();\n  char * result = (char *)key_name.c_str()\n';
  var code = "result";
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}


//初始化双路电机驱动
Blockly.Arduino['motorDriverBoard_dcMotorDriver'] = function(block) {
  var dropdown_driverPin = this.getFieldValue('driverPin');
  // var dropdown_motor1 = this.getFieldValue('motor1');
  // var dropdown_motor2 = this.getFieldValue('motor2');
  var dropdown_phDcMotors1 = Blockly.Arduino.valueToCode(this, 'phDcMotors1', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_phDcMotors2 = Blockly.Arduino.valueToCode(this, 'phDcMotors2', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_motorDriver'] = 'PH20Port motorDriver('+dropdown_driverPin +');\n';
 var code = '';
  if(dropdown_phDcMotors1 > 0){
    code +=  '  motorDriver.aWrite1(abs('+ dropdown_phDcMotors1 +'));\n  motorDriver.aWrite2(0);\n';
  }else{
    code +=  '  motorDriver.aWrite2(abs('+ dropdown_phDcMotors1 +'));\n  motorDriver.aWrite1(0);\n';
  }
  if(dropdown_phDcMotors2 > 0){
    code +=  '  motorDriver.aWrite3(abs('+ dropdown_phDcMotors2 +'));\n  motorDriver.aWrite4(0);\n';
  }else{
    code +=  '  motorDriver.aWrite4(abs('+ dropdown_phDcMotors2 +'));\n  motorDriver.aWrite3(0);\n';
  }
  return code;
  }


  //初始化ds1302
Blockly.Arduino['motorDriverBoard_initds1302'] = function() {
  var dropdown_port =  this.getFieldValue('ds1302Port');
  Blockly.Arduino.definitions_['include_initds1302'] = '#include <DS1302.h>';
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_DS1302' + dropdown_port] = 'PH20Port ds1302(' + dropdown_port + ');\n';
  Blockly.Arduino.definitions_['initds1302'] = 'DS1302 myRTC(ds1302.pin5(), ds1302.pin4(), ds1302.pin3());\n';
  return '';
  };
  //获取ds1302年月日时分秒
  Blockly.Arduino['motorDriverBoard_getds1302date'] = function() {
  var dropdown_DATES =  this.getFieldValue('DATES');
  var code = 'myRTC.'+dropdown_DATES+'()';
   return [code, Blockly.Arduino.ORDER_ATOMIC];
  };
  //ds1302设置时分秒
  Blockly.Arduino['motorDriverBoard_setds1302time'] = function() {
  var value_hour = Blockly.Arduino.valueToCode(this, 'hour', Blockly.Arduino.ORDER_ATOMIC);
  var value_minute = Blockly.Arduino.valueToCode(this, 'minute', Blockly.Arduino.ORDER_ATOMIC);
  var value_second = Blockly.Arduino.valueToCode(this, 'second', Blockly.Arduino.ORDER_ATOMIC);
  return 'myRTC.setTime('+ value_hour + ', ' + value_minute + ', ' + value_second + ');\n';
  };
  //ds设置年月日
  Blockly.Arduino['motorDriverBoard_setds1302date'] = function() {
  var value_year = Blockly.Arduino.valueToCode(this, 'year', Blockly.Arduino.ORDER_ATOMIC);
  var value_month = Blockly.Arduino.valueToCode(this, 'month', Blockly.Arduino.ORDER_ATOMIC);
  var value_day = Blockly.Arduino.valueToCode(this, 'day', Blockly.Arduino.ORDER_ATOMIC);
  return 'myRTC.setDate(' + value_day +', '+ value_month +', '+ value_year +');\n';
  };














    return Blockly;
}

module.exports = addArduinoCod ;
